官术网_书友最值得收藏!

  • React Cookbook
  • Carlos Santana Roldan
  • 213字
  • 2021-07-16 17:49:35

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.

  1. We need to create a new React app (check the last recipe if you haven't created a React app yet)
  1. Currently, our React application directory tree looks like this:
  1. We need to create src/components and src/shared directories
  2. After this, we need to create the src/components/Home directory for our component and move Home.js into this folder
  3. The App.js file stays at the src/components level
  4. Also, App.css and App.test.js will stay at src/components level
  5. Move the logo.svg file to src/shared/images
  6. Our index.js will stay at the src/ level
  7. 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.
  1. 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
  1. 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

主站蜘蛛池模板: 莆田市| 塘沽区| 岱山县| 威海市| 泸州市| 西平县| 济宁市| 容城县| 博野县| 常德市| 延庆县| 宣汉县| 布拖县| 綦江县| 崇左市| 高邮市| 息烽县| 喀喇沁旗| 景宁| 喀喇沁旗| 上高县| 叙永县| 都江堰市| 沙雅县| 莱州市| 潞西市| 英吉沙县| 广东省| 云浮市| 凤翔县| 伊宁市| 金川县| 清镇市| 尤溪县| 霍林郭勒市| 株洲县| 松阳县| 左云县| 大同市| 安阳市| 曲周县|