React Project Description
A typical React project consists of multiple files and folders, each with its specific role.
This chapter will give a basic description of React project code to help everyone understand the project structure and the function of each part.
-
A React project consists of multiple files and folders; the core files include
index.html、index.jsandApp.js。 -
React components are the basic building blocks of an application and can be function components or class components.
-
JSX is the core syntax of React, used to describe the UI.
-
Props and State are the main ways to manage data in React components.
Project Structure
A React project usually contains the following files and folders:
my-react-app/
├── node_modules/ # 项目依赖的第三方库
├── public/ # 静态资源文件夹
│ ├── index.html # 应用的 HTML 模板
│ └── ... # 其他静态资源(如图片、字体等)
├── src/ # 项目源代码
│ ├── App.js # 主组件
│ ├── App.css # 主组件的样式
│ ├── index.js # 项目入口文件
│ ├── index.css # 全局样式
│ └── ... # 其他组件和资源
├── package.json # 项目配置和依赖管理
├── package-lock.json # 依赖的精确版本锁定文件
└── README.md # 项目说明文档
Core File Analysis
public/index.html
public/index.html is the HTML template file of the React application.
React will render components into it.
React will render components into
it.
Example
React App
src/index.js
src/index.js
is the entry file of the React application.
src/index.js responsible for rendering the root component (usually App) into div#root in index.html.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom'
;
import
App from
'./App'
;
import
'./index.css'
;
ReactDOM.
render
(
<
React.
StrictMode
<
App
/>
React.
StrictMode
,
document.
getElementById
(
'root'
)
)
;
src/App.js
src/App.js is the root component of the React application.
src/App.js usually contains the main logic and structure of the application.
Example
import
React from
'react'
;
import
'./App.css'
;
function
App
(
)
{
return
(
<
div className
=
"App"
<
h1
Hello
,
React!
h1
div
)
;
}
export
default
App
;
src/App.css
**src/App.css ** is the style file of the App component.
Example
.App
{
text-align
:
center
;
margin-top
:
50px
;
}
src/index.css
src/index.css is the global style file, usually used to define global styles or reset browser default styles.
Example
body
{
margin
:
0
;
font-family
:
Arial
,
sans-serif
;
}
package.json
package.json This is the project's configuration file, containing the project's metadata, dependencies, and scripts.
Example
{
"name"
:
"my-react-app"
,
"version"
:
"1.0.0"
,
"scripts"
:
{
"start"
:
"react-scripts start"
,
// start the dev server
"build"
:
"react-scripts build"
,
// build production code
"test"
:
"react-scripts test"
,
// Run tests
"eject"
:
"react-scripts eject"
// Eject configuration (advanced usage)
}
,
"dependencies"
:
{
"react"
:
"^18.2.0"
,
"react-dom"
:
"^18.2.0"
,
"react-scripts"
:
"5.0.1"
}
}
The basic structure of React components
React components are the core building blocks of a React application.
A component usually contains the following parts:
1. Function components
Components defined using functions.
Example
import
React from
'react'
;
function
MyComponent
(
)
{
return
(
<
div
<
h2This is a component defined by a function
h2
div
)
;
}
export
default
MyComponent
;
Explanation:
-
import React from 'react';: imports the React library, which is a JavaScript library for building user interfaces. -
function MyComponent() { ... }: this is a function component. A function component is a JavaScript function that returns JSX. -
return (...): the component'sreturnstatement returns a JSX element. JSX is a syntax extension of JavaScript that allows you to write HTML-like code in JavaScript. -
export default MyComponent;: export theMyComponentcomponent as the default export. In this way, other files can useimport MyComponent from './MyComponent';to import and use this component.
2. Class components
Components defined using classes (now less commonly used; function components are recommended).
Example
import
React
,
{
Component
}
from
'react'
;
class
MyComponent
extends
Component
{
render
(
)
{
return
(
<
div
<
h2This is a class component
h2
div
)
;
}
}
export
default
MyComponent
;
Explanation:
-
import React from 'react';: import the React library, which is the basis of using React. -
import { Component } from 'react';: import from the React libraryComponentclass.Componentis the base class of React class components; all class components need to inherit from it. -
class MyComponent extends Component { ... }: defines a class component namedMyComponent. It inherits fromComponentclass, so it can use the features of React class components (such as lifecycle methods, state management, etc.). -
render() { ... }:renderis a method that must be implemented in a class component. It is responsible for returning the component's UI structure (JSX). -
return (...): returns a JSX element describing the component's UI. -
: this is a simple JSX structure containing a这是一个类组件
divand oneh2Headings.
return (...): returns a JSX element describing the component's UI.
: this is a simple JSX structure containing a 这是一个类组件
div and one h2 Headings.
-
export default MyComponent;: export theMyComponentcomponent as the default export. In this way, other files can useimport MyComponent from './MyComponent';to import and use this component.
3、JSX
JSX is a syntax extension of JavaScript that allows writing HTML-like code in JavaScript.
Example
const
element
=
<
h1
Hello
,
JSX!
h1
;
4、Props
Props are the input parameters of a component, used to pass data from a parent component to a child component.
Example
function
Welcome
(
props
)
{
return
<h1 Hello,{>props.
name
}
h1
;
}
function
App
(
)
{
return
<
Welcome name
=
"React"
/>;
}
5、State
State is the internal state of a component, used to manage the component's data.
Example (using the useState Hook)
import
React
,
{
useState
}
from
'react'
;
function
Counter
(
)
{
const
[
count
,
setCount
]
=
useState
(
0
)
;
return
(
<
div
<
p
Count:
{
count
}
p
<
button onClick
=
{
(
)
=>
setCount
(
count
1
)
}
Increment
button
div
)
;
}
export
default
Counter
;
The running process of a React project
Start the Dev Server
Run npm start or yarn start to start the development server.
Open a browser and visit http://localhost:3000 to view the application.
Build Production Code
Run npm run build or yarn build to generate optimized production code.
Running tests
Run npm test or yarn test to execute the project's test cases.