Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

React Project Description

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.

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

 

   

   

   

<p>React App</p>

 

 

   

 

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

<

h2

This is a component defined by a function

h2

   

div

 

)

;

}

export

default

MyComponent

;

Explanation:

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

<

h2

This is a class component

h2

     

div

   

)

;

 

}

}

export

default

MyComponent

;

Explanation:

-

return (...): returns a JSX element describing the component's UI.

-

这是一个类组件

: this is a simple JSX structure containing a div and one h2 Headings.

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.