在不调度的情况下调用的操作 - Redux



我试图理解redux,并且对mapDispatchToProps有一些疑问。

代码如下。

import * as actions  from '../../redux/actions';
import './style.scss'
class Center extends Component {
  componentDidMount() {
    this.props.getCenters();
  }
  ...
}
export default connect(state => ({
  ...state.center,
}),{
  ...actions,
})(Center);

操作文件定义如下。

export const getCenters = () => ({
  type: types.CENTERS_REQUEST,
});

挂载组件时,将调用 this.props.getCenters(( 并调度操作,并且一切都按预期工作。

问题是,

getCenters的定义中没有提到调度。那么,如何在不调度的情况下调用此操作呢?

Redux connect中(隐式地(将dispatch注入到您的操作中:

export default connect(state => ({
  ...state.center,
}),{
  ...actions,
})(Center);

如果我们稍微抽象一下,我们可以观察它是如何发生的(明确地(:

function mapDispatchToProps(dispatch) {
    return {
        getCenters: () => dispatch(getCenters())
    }
}
export default connect(state => ({
  ...state.center,
}), mapDispatchToProps)(Center);

结果,getCenters 配备了 dispatch .

最新更新