![React Cookbook](https://wfqqreader-1252317822.image.myqcloud.com/cover/515/36699515/b_36699515.jpg)
上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:
![](https://epubservercos.yuewen.com/B3A39D/19470389501544606/epubprivate/OEBPS/Images/92e8684d-42cf-416f-a60a-5afcff61c84b.png?sign=1738752040-5NLFChoQDnGt026IkknBclcc8HSJryuN-0-ee3e8249c86497a9e52cdc0d1126f03b)
- 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:
![](https://epubservercos.yuewen.com/B3A39D/19470389501544606/epubprivate/OEBPS/Images/3ba9e4f5-31dd-4ec2-b313-a65fc5056d59.png?sign=1738752040-qEeb1gLunR9OClnvK37wHZj2x7BOgwrm-0-b7078ced1834c1d53f56ec275b2dffb0)
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