上QQ阅读APP看书,第一时间看更新
How to do it...
We can create React components with the default structure that create-react-app provides, but in this recipe, I'll show you a better way to organize the project so that we are ready when for when the application grows.
- We need to create a new React app (check the last recipe if you haven't created a React app yet)
- Currently, our React application directory tree looks like this:
- We need to create src/components and src/shared directories
- After this, we need to create the src/components/Home directory for our component and move Home.js into this folder
- The App.js file stays at the src/components level
- Also, App.css and App.test.js will stay at src/components level
- Move the logo.svg file to src/shared/images
- Our index.js will stay at the src/ level
- Now your directory tree should look like this:
I highly recommend that you create another directory for shared components, src/shared/components. I'll explain more about this in the next recipes.
- In the App.js file, change the logo and Home imports:
import logo from '../shared/images/logo.svg';
import Home from './Home/Home';
File: src/components/App.js
- After you changed that, we need to open the index.js and fix the import path for the App component:
import App from './components/App';
File: src/index.js