作者:哈哈不会玩NO1 | 来源:互联网 | 2023-01-15 15:03
如何解决《ReactRedux-在已连接的组件中使用mapDispatchToProps时如何在componentDidMount上调度动作》经验,为你挑选了1个好方法。
1> Tomasz Mular..:
我将您的代码修改为我认为可行的代码。我也发表了评论。
class App extends Component {
state = {
posts: []
}
componentDidMount() {
// no need to use dispatch again. Your action creators are already bound by
// mapDispatchToProps. Notice also that they come from props
const { selectedCategory, fetchCategoriesIfNeeded, fetchPostsIfNeeded} = this.props;
fetchCategoriesIfNeeded(selectedCategory);
fetchPostsIfNeeded(selectedCategory);
}
//... the same
}
function mapStateToProps ( state ) {
//... the same
}
function mapDispatchToProps (dispatch) {
// when arguments match, you can pass configuration object, which will
// wrap your actions creators with dispatch automatically.
return {
orderPost,
fetchCategoriesIfNeeded,
fetchPostsIfNeeded,
}
}