1. Introduction to the historical background and features of React
- The traditional
Web UI
roots of development problems, as follows:
- Traditional
UI
operating too much attention to detail - Application status is scattered everywhere, difficult to track and maintain
-
Traditional
DOM API
focus too much detail,React
always refresh the whole page, do not care about the details -
React
It's very simple, as shown below:
- 1 new concept
- 4 must
API
- Unidirectional data flow
- Complete error message
React
Solve theUI
details, use the data modelFlux
architecture, unidirectional data flow
Second, consider the construction of UI in a component way
-
Contemplate a component
UI
, theUI
organized as a tree of components -
Appreciated that
React
components as follows:
porps + state = view
React
Components generally do not provide methods, but some kind of state machineReact
The component can be understood as a pure function- One-way data binding
- Create a simple component
TabSelect
, as follows:
- Create static
UI
- Consider the state of the component
- Consider how components interact
- Controlled and uncontrolled components are as follows:
- Controlled components, the state of form elements is maintained by the user
- Uncontrolled components form elements state
DOM
itself Maintenance
- When to create a component, the single responsibility principle is as follows:
- Each component only does one thing
- If the component becomes complex, it should be split into small components
- The
DRY
principles of data state management are as follows:
- Don’t store the calculated state separately
- Stateless assembly possible, the required data
props
acquisition
3. The essence of JSX is not a template engine, but syntactic sugar
-
Understanding
JSX
is not a template language, but a syntactic sugar -
JSX
InJavascript
the code written directlyHTML
mark -
JSX
The essence of the syntactic sugar for dynamically creating components -
In
JSX
using the expression, as follows:
JSX
Is an expression itself- Use expressions in attributes
- Extended attributes
- Expression as child element
JSX
The advantages are as follows:
- Intuitiveness of the declarative creation interface
- Flexible code creation interface
- No need to learn a new template language
- By convention, custom components start with a capital letter, as shown below:
React
I think lowercasetag
nativeDOM
node, such asdiv
- Custom components start with capital letters
JSX
Markers can use attribute syntax directly, for example<menu.Item />
Fourth, the life cycle of React components and their usage scenarios
react
The life cycle is as follows:
Render
Stage, pure and no side effects, may bereact
suspended, suspend or restartPre-commit
Stage, you can readDOM
Commit
Stages, can be usedDOM
, run side effects, schedule updates
constructor
,As follows:
- Used to initialize the internal state, rarely used
- The only directly modify the
state
place
getDerivedStateFromProps
,As follows:
- When
state
required from theprops
use initialization - Try not to use it, maintaining the consistency of the two states will increase complexity
- Each
render
will call - In typical scenarios, form controls get default values
componentDidMount
,As follows:
UI
Called after rendering is complete- Only execute once
- Typical scenario, obtaining external resources
componentWillUnmount
,As follows:
- Called when the component is removed
- Typical scenario, resource release
getSnapshotBeforeUpdate
,As follows:
- In the page
render
before the callstate
has been updated - A typical scene, acquired
render
before theDOM
state
componentDidUpdate
,As follows:
- Each time
UI
is called when the update - A typical scenario, the page need to
props
change retrieve data
shouldComponentUpdate
,As follows:
- Decide
Virtual DOM
whether to redraw - Generally it is made
PureComponent
automatically - Typical scenarios, performance optimization