如果我创建自己的mapDispatchToProps函数,它就不能工作。如果我为connect提供一个普通的对象,那么它确实能工作,但是我需要调度功能。对于每一页的翻译,我是不是做错了什么?
const mapStateToProps = (state) => {
const { isFetching, lastUpdated, items, errors } = state.transactions; // fetch from redux state ;)
return {
translate: getTranslate(state.locale),
isFetching,
lastUpdated,
items,
errors
}
}
const mapDispatchToProps = dispatch => {
return {
fetchTransactionsIfNeeded,
invalidateList
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Transactions);下面的代码起作用
const mapStateToProps = (state) => {
const { isFetching, lastUpdated, items, errors } = state.transactions; // fetch from redux state ;)
return {
translate: getTranslate(state.locale),
isFetching,
lastUpdated,
items,
errors
}
}
export default connect(mapStateToProps, {
fetchTransactionsIfNeeded,
invalidateList
})(Transactions);发布于 2017-12-25 15:54:14
根据剩余的文档
mapDispatchToProps(dispatch, [ownProps]): dispatchProps如果传递了一个对象,则假定其中的每个函数都是Redux操作创建者。一个具有相同函数名的对象,但是每个动作创建者都封装在一个分派调用中,以便可以直接调用它们,将被合并到组件的道具中。 如果传递一个函数,它将作为第一个参数被赋予分派。返回一个以自己的方式使用分派绑定动作创建者的对象是由您自己决定的。(提示:您可以使用来自Redux的bindActionCreators()助手。)
在第一种情况下,当您实现mapDispatchToProps时,您将返回一个普通的对象,但是您需要在其中使用调度,因为它本身并不是由redux作为动作创建者来假定的。
你会实现它就像
const mapDispatchToProps = dispatch => {
return {
fetchTransactionsIfNeeded: (...args) => {
dispatch(fetchTransactionsIfNeeded(...args))
},
invalidateList: (...args) => {
dispatch(invalidateList(...args))
},
}
}否则,不要将其创建为函数,而只是创建一个对象。
const mapDispatchToProps = {
fetchTransactionsIfNeeded,
invalidateList
}https://stackoverflow.com/questions/47969692
复制相似问题