对于每个组件,我都有名为 actions.js
的 Action 创建者文件。在 ComponentOne
中,我有:
let actions = {
logSaying() {
console.log("Hi there");
}
}
export default actions
然后我有一个主要 Action 创建者文件,并尝试将所有 Action 放在一张表中,如下所示:
import componentOneActions from './ComponentOne/actions'
let actions = {
componentOneActions,
//componentTwoActions,
//etc.
}
export default actions
并且会像这样从主要 Action 创建者表中将 Action 传递给所有组件:
import actions from './actions'
...
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch)
}
}
但是当我尝试 console.log()
时,没有任何操作被传递下来。为什么它没有被传递下来,我怎样才能形成一个 Action 创作者表?实际上有更好的方法吗?
预先感谢您并将投票/接受答案。
最佳答案
更新
之前的答案是错误的。
也许您以错误的方式使用了actions
。
来自 document , bindActionCreators(actionCreators, dispatch)
接受第一个参数作为 actionCreators
。
actionCreators
是一个像这样的对象:
function addTodo(text) {
return {
type: ADD_TODO,
text
}
}
http://redux.js.org/docs/basics/Actions.html#action-creators
所以你的actions
不完全是actionCreators
。
您应该将 console.log()
放入您的 reducer 中,并使 actions
成为文件所示的简单对象。
function todoApp(state = initialState, action) {
switch (action.type) {
case ADD_TODO:
console.log();
return {}
default:
return state
}
}
http://redux.js.org/docs/basics/Reducers.html#handling-more-actions
关于javascript - Redux:如何将 action creators 编译成一个 action creators 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971138/