我遇到了一个怪异的问题,我可以正确地调度动作,但是未调用reducer。我正在使用redux-thunk,所有设置都与在其他项目之一中设置的完全相同。动作类型匹配,但是什么也没发生。
此外,如果将console.log放在化简器的默认情况下,则每次刷新页面时都会记录4次,因此我认为化简器已正确加载。
export const submitForm = names => {
console.log(names); //This logs what it's supposed to log every time
// I press the submit button
return dispatch => {
dispatch({
type: "SUBMIT_FORM",
names
});
};
};
export default function form(state = {}, action) {
switch (action.type) {
case "SUBMIT_FORM":
console.log(action.names);
return Object.assign({}, state, {
names: action.names
});
default:
return state;
}
}
//My component is connected like this:
const mapStateToProps = state => ({});
const mapDispatchToProps = dispatch => ({
formActions: bindActionCreators(formActions, dispatch)
});
export default connect(mapStateToProps, mapDispatchToProps)(Home);
答案 0 :(得分:0)
action creator
返回一个函数对我来说有点奇怪。 Action creators
通常return
是一个动作,即object
。 Async action creators
返回functions
。因此,您可以尝试重构action creator
的{{1}},然后将操作创建者作为参数传递给调度。
return a objetct
此外,根据react-redux.js.org/mapdispatch在export const submitForm = names => {
console.log(names); //This logs what it's supposed to log every time
// I press the submit button
return {
type: "SUBMIT_FORM",
names
}
};
dispatch(submitForm(names));
中使用bindActionCreators
,mapDispatchToProps
不会返回对象,但会返回mapDispatchToProps
。
bindActionCreators
另外一点,如果您需要将参数传递给const mapDispatchToProps = dispatch =>
bindActionCreators(formActions, dispatch);
,我会这样做:
action creator
很遗憾,我看不到您的所有代码。希望我能对您有所帮助。