简体   繁体   中英

React-Redux connect

I have two queries regarding react-redux connect

the connect can be called with two arguments mapStateToProps, mapDispatchToProps

  1. Please Correct me if I am wrong, <Provider store={store}> </Provider>
    In mapStateToProps (first argument in connect())
    mapStateToProps = ( state ) => ({....})

    state over here is the same as store.getState(); ??
    please provider a bit more explaination to this
  2. Please explain the usage of mapDispatchToProps with a beginner's example.

mapDispatchToProps is a function that gets dispatch function as argument. You're expected to return an object, where you map keys with a function to dispatch an action.

Example time:

connect(/* ...*/, function(dispatch) {
    return {
        sayHello: function() { dispatch("ACTION_TYPE_HELLO")}
    }
   }

Connected components will receive prop sayHello , which is a function, where this.props.sayHello() will dispatch an action ACTION_TYPE_HELLO to the store.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM