简体   繁体   English

React-Redux 连接

[英]React-Redux connect

I have two queries regarding react-redux connect我有两个关于 react-redux connect 的问题

the connect can be called with two arguments mapStateToProps, mapDispatchToProps可以使用两个参数 mapStateToProps、mapDispatchToProps 调用连接

  1. Please Correct me if I am wrong, <Provider store={store}> </Provider>如果我错了,请纠正我, <Provider store={store}> </Provider>
    In mapStateToProps (first argument in connect())在 mapStateToProps 中(connect() 中的第一个参数)
    mapStateToProps = ( state ) => ({....}) mapStateToProps = ( state ) => ({....})

    state over here is the same as store.getState();这里的state和 store.getState() 一样; ?? ??
    please provider a bit more explaination to this请提供者对此进行更多解释
  2. Please explain the usage of mapDispatchToProps with a beginner's example.请用初学者的例子解释一下 mapDispatchToProps 的用法。

mapDispatchToProps is a function that gets dispatch function as argument. mapDispatchToProps是一个以dispatch函数为参数的函数。 You're expected to return an object, where you map keys with a function to dispatch an action.你应该返回一个对象,在那里你用一个函数map键来调度一个动作。

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.连接的组件将接收 prop sayHello ,这是一个函数, this.props.sayHello()将向 store 发送一个动作ACTION_TYPE_HELLO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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