Home>Article>Web Front-end> How does redux relate to react?
In react, the react-redux library can be associated with redux; react-redux provides some encapsulation, which can organize the code in a more scientific way, allowing us to use Redux in React code more comfortably.
The operating environment of this tutorial: windows7 system, React17 version. This method is suitable for all brands of computers.
Redux itself is not related to React. It is a general Javscript App module used for App State management. To use Redux in a React project, a better way is to use the react-redux library to connect. What this means is that without react-redux, these two libraries will not be used together. react-redux provides some encapsulation, a more scientific way of organizing code, allowing us to use Redux in React code more comfortably.
How do redux and react work together?
react-redux provides two basic friends: connect and provider. They connect the component and redux. The provider passes the store to the component. The component issues an action through dispatch. The store is based on the action. The type attribute calls the corresponding reducer and passes in the state and this action. The reducer processes the state and returns a new state and puts it into the store. Connect monitors the changes in the store and calls setState to update the component. At this time, the props of the component also change accordingly. .
Provider:
Provider is a component that accepts store as props and then passes it down through context. Any component in this react can obtain store through context;
connect:
connect(mapStateToProps, mapDispatchToProps, mergeProps, options) is a function that accepts four parameters and returns a function—wrapWithConnect. wrapWithConnect accepts a The component is used as a parameter wrapWithConnect(component), which internally defines a new component connect (container component) and uses the incoming component as a subcomponent of connect and returns it.
mapStateToProps(state.[ownPros]):
mapStateToProps accepts two parameters, store state and custom props, and returns a new object, this The object is passed to the UI component as part of props. We can customize an object to be returned based on the data required by the component. Changes in ownProps will also trigger mapStateToProps
mapDispatchToProps(dispatch,[ownProps]):
mapDispatchToProps if If it is an object, it will be bound to the store and passed to the UI component as part of the props. If this function accepts two parameters, bindActionCreators will bind action and dispatch and return an object. This object will be passed into the UI component together with ownProps as part of props, so whether mapDispatchToProps is an object or a function, it will eventually return an object. , if it is a function, the key value of this object can be customized.
For more programming-related knowledge, please visit:Programming Teaching! !
The above is the detailed content of How does redux relate to react?. For more information, please follow other related articles on the PHP Chinese website!