React Tutorial
React is a JAVASCRIPT library for building user interfaces.
React is mainly used for building UIs, and many people consider React to be the V (View) in MVC.
React originated from an internal project at Facebook, used to build Instagram's website, and was open-sourced in May 2013.
React has high performance and very simple code logic, and more and more people have begun to pay attention to and use it.
React Features
-
1. Declarative design −React adopts a declarative paradigm, making it easy to describe applications.
-
2. Efficient −React minimizes interaction with the DOM by simulating it.
-
3. Flexible −React works well with known libraries or frameworks.
-
4.JSX − JSX is an extension of JavaScript syntax. React development does not necessarily require JSX, but we recommend using it.
-
5. Components − Building components with React makes code easier to reuse and works well in the development of large projects.
-
6. Unidirectional reactive data flow − React implements unidirectional reactive data flow, reducing repetitive code, which is why it is simpler than traditional data binding.
Knowledge you need before reading this tutorial:
Before starting to learn React, you need the following basic knowledge:
React First Example
In each chapter, you can edit examples online and then click a button to view the results.
This tutorial uses React version 18.2.0; you can visit the official website https://react.dev/ to download the latest version.
React example
<
div
id
=
"
example
"
div
<
script
type
=
"
text/babel
"
// Simple React component function App() { return
<
h1
Hello, React!
h1
; } const root = ReactDOM.createRoot(document.getElementById("example")); // render the React component to the DOM root.render(
<
App
/>
);
script
Try it »
** Importing External Scripts:**
These three lines of code import the React, ReactDOM, and Babel Standalone libraries respectively.
- React is used for building user interfaces.
- ReactDOM is used to render React components in the browser.
- Babel Standalone is used to compile JSX syntax on the fly in the browser.
Or a React development environment created using the create-react-app tool (introduced in the next chapter):
Example
import
React from
"react"
;
import
ReactDOM from
"react-dom/client"
;
function
Hello
(
props
)
{
return
<
h1
Hello World
!
h1
;
}
const
root
=
ReactDOM.
createRoot
(
document.
getElementById
(
"root"
)
)
;
root.
render
(
<
Hello
/>
)
;
At this point, open the browser http://localhost:3000/ and it will output:
Hello World!
References
- React official website:https://react.dev/
- React Chinese documentation:https://zh-hans.react.dev/
- React GitHub source code:https://github.com/facebook/react