React Installation (NPM)
Before you begin, make sure Node.js and npm are installed. You can check whether they are installed using the following command:
node -v
npm -v
If your system does not yet support Node.js and NPM, you can refer to our Node.js Tutorial。
We recommend using the CommonJS module system with React, such as browserify or webpack; this tutorial uses webpack.
In China, npm can be slow; you can use Taobao's customized cnpm (with gzip compression support) command-line tool to replace the default npm:
$ npm install -g cnpm --registry=https://registry.npmmirror.com
$ npm config set registry https://registry.npmmirror.com
You can then use the cnpm command to install modules:
$ cnpm install [name]
For more information, see:http://npm.taobao.org/。
Using create-react-app to quickly build a React development environment
React provides an official tool, Create React App, for quickly setting up React projects.
create-react-app comes from Facebook; with this command we can quickly build a React development environment without configuration.
The project automatically created by create-react-app is based on Webpack + ES6.
Run the following command to create the project:
$ cnpm install -g create-react-app
$ create-react-app my-app
$ cd my-app/
$ npm start
You can also use npx command to create it; npx is a tool included in npm 5.2.0 and later, used to execute local or remote npm packages:
npx create-react-app my-app
Open in the browser http://localhost:3000/ , the result is shown below:

The project's directory structure is as follows:
my-first-react-app/
├── node_modules/
├── public/
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
├── src/
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ ├── reportWebVitals.js
│ └── setupTests.js
├── .gitignore
├── package.json
├── README.md
└── yarn.lock (或 package-lock.json)
Directory and file descriptions
node_modules/
Stores all the project's dependency packages. This directory is automatically generated by npm or yarn and contains all the third-party libraries and modules needed to run the project.
public/
Stores static files; Webpack does not process the files in this directory.public Files in this directory are copied directly to the build directory.
favicon.ico: the small icon on the browser tab.index.html: the HTML template file; React components will be mounted into the root node in this filedivelement.logo192.pngandlogo512.png: React logo icons in different sizes.manifest.json: the web app manifest file, used to define metadata such as the app's name and icons.robots.txt: used to tell search engines which pages can be crawled.
src/
Stores the application's source code. This is where you do most of your development.
App.css:AppThe component's style file.App.js: the main component file, which defines a basic React component.App.test.js:AppThe component's test file.index.css: the global style file.index.js: the application's entry file, responsible for rendering React components into the DOM.logo.svg: the SVG file of the React logo.reportWebVitals.js: a file for performance monitoring, which helps you understand and analyze the application's performance.setupTests.js: a file used to set up the testing environment.
.gitignore
Lists files and directories that Git should ignore, such as node_modules/ and the build output directories.
package.json
The project's configuration file, containing project information, scripts, dependencies, etc.
README.md
The project's README file, containing basic information and usage instructions for the project.
yarn.lock** or package-lock.json**
lock file, which records the exact dependency versions to ensure consistent installations across different environments.
Try modifying the code in the src/App.js file:
src/App.js
import
React
,
{
Component
}
from
'
react
'
;
import
logo
from
'
./logo.svg
'
;
import
'
./App.css
'
;
class
App
extends
Component
{
render
(
)
{
return
(
<
div
className
=
"
App
"
<
div
className
=
"
App-header
"
<
img
src
=
{
logo
}
className
=
"
App-logo
"
alt
=
"
logo
"/> <
h2
Welcome to Runoob Tutorial
h2
div
< p className = " App-intro "You can <
code
src
/
App
.
js
code
modify it in the file.
p
div
)
;
}
}
export
default
App
;
After modifying, open http://localhost:3000/ (it usually refreshes automatically), and the output is as follows:

src/index.js is an entry file; we can try directly modifying the code in src/index.js:
Note: In React 18, ReactDOM.render is deprecated.
Example
import
React from
'react'
;
import
ReactDOM from
'react-dom'
;
function
Hello
(
props
)
{
return
<
h1
Hello
{props.
name
}
!
h1
;
}
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
"root"
)
)
;
// Render the Hello component and pass the name prop
root.
render
(
<
Hello name
=
"World"
/>
)
;
At this point, open the browser http://localhost:3000/ and it will output:
Hello World!
Creating your first component
You can edit App.js file to create your own first component. Here is a simple example that changes the App component to display "Hello, React!":
Example
import
React from
'react'
;
import
'./App.css'
;
function
App
(
)
{
return
(
<
div className
=
"App"
<
header className
=
"App-header"
<
h1
Hello
,
React!
h1
header
div
)
;
}
export
default
App
;
After saving the file, the browser will refresh automatically, and you should see "Hello, React!" displayed on the page.
You can src Create more component files in the directory, for example, create a Hello.js File:
Hello.js file with the following code:
import
React from
'react'
;
function
Hello
(
)
{
return
<
h1
Hello from a
new
component
!
h1
;
}
export
default
Hello
;
Then in App.js import and use this new component in it:
Example
import React from 'react';
import './App.css';
import Hello from './Hello';
function App() {
return (
);
}
export default App;
After saving the file, the browser will refresh automatically, and you will see the content of the new component.