1. Why does the Redux front end need a state management library?
-
Redux
,JS
State regulatory framework,Redux
make communication easier assembly, putstore
on the assembly, all components andstore
carry out transit -
Redux
The characteristics are as follows:
- Unidirectional data flow
- Predictability,
state + action = new state
- Pure function update
Store
Second, Redux has an in-depth understanding of Store, Action and Reducer
store
BycreateStore
incomingreducer
created, comprising three methods, as follows:
getState()
To get the current statusdispatch(action)
, Distribute oneaction
subscribe(listener)
, Monitorstore
changes
(state,action) => new state
,As follows:
Store
Actions
Reducer
View
-
combineReducers
Capable of accepting a plurality ofreducer
post as a parameter, it returns a final packagereducer
-
bindActionCreators
, Single or multipleActionCreator
transformed intodispatch(action)
a set of functions in the form of, not manuallydispatch(actionCreator(type))
, but may directly invoke a method
Three, use Redux in React
connect
Working principle of high-end components
Four, Redux understands asynchronous Action, Redux middleware
(state,action) => new state
,As follows:
Store
Actions
Reducer
View
Middleware
Redux
Middleware,Middleware
as shown below:
- Intercepted
action
- issue
action
-
Asynchronous
action
not specialaction
but a plurality of synchronizationaction
combination use -
Middleware
dispatcher
interceptedaction
special treatment
5. How Redux organizes Action and Reducer
- Standard form
Redux Action
of questions, as follows:
- All
Action
put a file, unlimited expansion Action、Reducer
Separate, need to switch back and forth when implementing business logic- What are the system
Action
intuitive enough
-
The new way, a single
action
andreducer
in the same file -
The new method, one for each file
Action
, is as follows:
- Ease of development, not in
action
andreducer
switch back and forth between documents - Ease of maintenance, each
action
file is very small, easy to understand - Easy to test, each business logic only needs to correspond to one test file
- Easy to understand, the file name is the
action
list is the name of the fileaction
list
Six, Redux understands immutability of immutable data
- Immutable data
immutable data
, immutable data is required, as shown below:
- Performance optimization
- Easy to debug and track
- Easy to guess
- How to manipulate immutable data is as follows:
- Native writing,
{...}, Object.assign
immutability-helper
immer