mapStateToProps
2017-01-19 13:03
405 查看
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options])
Connects a React component to a Redux store. connectis a facade around
connectAdvanced,
providing a convenient API for the most common use cases.
It does not modify the component class passed to it; instead, it returns a new, connected component class for you to use.
Arguments
[mapStateToProps(state, [ownProps]): stateProps] (Function):
If this argument is specified, the new component will subscribe to Redux store updates. This means that any time the store is updated,
mapStateToPropswill
be called. The results of
mapStateToPropsmust be a plain object*, which will be merged into the component’s props. If you
don't want to subscribe to store updates, pass
nullor
undefinedin
place of
mapStateToProps. If
ownPropsis
specified as a second argument, its value will be the props passed to your component, and
mapStateToPropswill be additionally
re-invoked whenever the component receives new props (e.g. if props received from a parent component have shallowly changed, and you use the ownProps argument, mapStateToProps is re-evaluated).
Note: in advanced scenarios where you need more control over the rendering performance,
mapStateToProps()can
also return a function. In this case, that function will be used as
mapStateToProps()for a particular
component instance. This allows you to do per-instance memoization. You can refer to #279 and the
tests it adds for more details. Most apps never need this.
The
mapStateToPropsfunction takes a single argument of the entire Redux store’s
state and returns an object to be passed as props. It is often called a selector. Use reselect to
efficiently compose selectors and compute derived data.
[
mapDispatchToProps(dispatch, [ownProps]): dispatchProps] (Object or Function):
If an object is passed, each function inside it is assumed to be a Redux action creator. An object with the same function names, but with every action creator wrapped into a
dispatchcall
so they may be invoked directly, will be merged into the component’s props. If a function is passed, it will be given
dispatch.
If you don't want to subscribe to store updates, pass
nullor
undefinedin
place of
mapDispatchToProps. It’s up to you to return an object that somehow uses
dispatchto
bind action creators in your own way. (Tip: you may use the
bindActionCreators()helper
from Redux.) If you omit it, the default implementation just injects
dispatchinto your component’s props. If
ownPropsis
specified as a second argument, its value will be the props passed to your component, and
mapDispatchToPropswill be re-invoked
whenever the component receives new props.
Note: in advanced scenarios where you need more control over the rendering performance,
mapDispatchToProps()can
also return a function. In this case, that function will be used as
mapDispatchToProps()for a particular
component instance. This allows you to do per-instance memoization. You can refer to #279 and the
tests it adds for more details. Most apps never need this.
[
mergeProps(stateProps, dispatchProps, ownProps): props] (Function):
If specified, it is passed the result of
mapStateToProps(),
mapDispatchToProps(),
and the parent
props. The plain object you return from it will be passed as props to the wrapped component. You may specify
this function to select a slice of the state based on props, or to bind action creators to a particular variable from props. If you omit it,
Object.assign({}, ownProps, stateProps, dispatchProps)is used by default.
[
options] (Object) If specified, further customizes the
behavior of the connector. In addition to the options passable to
connectAdvanced()(see those below),
connect()accepts
these additional options:
[
pure] (Boolean): If true,
connect()will
avoid re-renders and calls to
mapStateToProps,
mapDispatchToProps,
and
mergePropsif the relevant state/props objects remain equal based on their respective equality checks. Assumes that the
wrapped component is a “pure” component and does not rely on any input or state other than its props and the selected Redux store’s state. Default value:
true
[
areStatesEqual] (Function): When pure, compares incoming store state to
its previous value. Default value:
strictEqual (===)
[
areOwnPropsEqual] (Function): When pure, compares incoming props to its
previous value. Default value:
shallowEqual
[
areStatePropsEqual] (Function): When pure, compares the result of
mapStateToPropsto
its previous value. Default value:
shallowEqual
[
areMergedPropsEqual] (Function): When pure, compares the result of
mergePropsto
its previous value. Default value:
shallowEqual
相关文章推荐
- mapStateToProps,mapDispatchToProps的使用详解
- React依赖注入说明(mapStateToProps/mapDispatchToProps)
- connect中的mapStateToProps该怎么写
- [置顶] React和Redux 之间的依赖注入connect(mapStateToProps、mapDispatchToProps)
- React Native基础&入门教程:以一个To Do List小例子,看props和state
- [Redux] Using mapDispatchToProps() Shorthand Notation
- [AngualrJS NG-redux] Map State and Dispatchers to Redux
- React Native基础&入门教程:以一个To Do List小例子,看props和state
- OGRE roadmap to 1.9 and 2.0
- ReactJS修炼之路(三):props vs state
- Systemstate dump when connection to the instance is not possible (文档 ID 359536.1)
- How to map remote network drive
- Notes: How to iterate a map in Java
- How to map SCN with Timestamp before 10g [ID 365536.1]
- mybatis中mapUnderscoreToCamelCase的使用
- failed to sync branch You might need to open a shell and debug the state of this repo
- Introduction to MapGuide OpenSource – from FOSS4G in Denver
- state-of-the-art implementations related to visual recognition and search
- Ubuntu elasticsearch max virtual memory areas vm.max_map_count [65530] is too low, increase to at le
- Operation is not valid due to the current state of object 错误解决