Javascript 我能';t将我的动作作为道具发送(React&;Redux)
下面是我如何使用Javascript 我能';t将我的动作作为道具发送(React&;Redux),javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,下面是我如何使用connect: function mapStateToProps(state, ownProps) { return { // we'll call this in our component -> this.props.listingData listingData: state.locations.listingData, //we'll call this in out component -> this.props.form
connect
:
function mapStateToProps(state, ownProps) {
return {
// we'll call this in our component -> this.props.listingData
listingData: state.locations.listingData,
//we'll call this in out component -> this.props.formState
formState: state.formStates.formState
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(locationActions, formStateActions, dispatch)
};
}
export default connect(mapStateToProps, mapDispatchToProps)(homePage);
这是我使用的按钮:
<div onClick={this.stateToEntry} className="addButton">Add</div>
我得到的错误是this.props.actions.stateToEntry()
不是函数。这里到底发生了什么&我该如何解决这个问题
编辑:
以下是日志数据:
换句话说,它只是在其周围添加{}
。我曾尝试单独使用{formStateActions}
,但它不起作用,但formStateActions
起作用
对于@LazarevAlexandr,这里是我的formStateActions的actioncreator
:
export function stateToEntry() {
return { type: types.STATE_TO_ENTRY, formState: 'entry-mode'};
}
export function stateToEdit() {
return { type: types.STATE_TO_EDIT, formState: 'edit-mode'};
}
export function stateToDelete() {
return { type: types.STATE_TO_DELETE, formState: 'delete-mode'};
}
我的位置Actions Creator很长,所以我不想在这里完全发布。它们都是函数,有些是返回操作的ActionCreator,有些返回从api获取数据列表的函数。
bindActionsCreators
只获取两个参数,因此如果要传递多个操作集,请尝试以下操作:
function mapDispatchToProps(dispatch) {
const actions = Object.assign({}, locationActions, formStateActions);
return {
actions: bindActionCreators(actions, dispatch)
};
}
我在使用
bindActionCreators({…locationActions,…formStateActions},dispatch)时遇到解析错误
但是在使用bindActionCreators({locationActions,formStateActions},dispatch)
时解析问题得到了解决。我仍然收到相同的错误->this.props.actions.stateToEntry()
不是一个函数,您需要babel-preset-stage2
用于对象rest spread,无论如何您可以尝试此bindActionCreators(object.assign({},locationActions,formStateActions),dispatch)
使用object.assign()
尝试您的建议,它不起作用。当我使用bindActionCreators(formStateActions,dispatch)
时,它就工作了。还有其他建议吗?试着调试执行对象时得到的结果。分配({},locationActions,formStateActions)
它必须是包含所有操作的对象。尝试console.log什么是locationActions
,它是对象吗?它是对象,但我似乎无法让它读取我的操作。你有办法解决这个问题吗?请显示你在哪里导入locationActions
和formStateActions
的代码,以及你是如何定义的them@LazarevAlexandr正如@krvital在他的回答中提到的,问题在于bindActionCreators
,因为我试图使用3个参数,而它应该是2。(嗯,我还需要检查文件)。我现在的问题是bindActionCreators({action1},dispatch)
不起作用。但是bindActionCreators(action1,dispatch)
没有括号可以工作,但这只是在使用1个动作或1个参数的情况下。我想使用多个动作。我已经理解了这一点,并且假设您的动作创建者可能有问题。我已经为您编辑了我的文章。
function mapDispatchToProps(dispatch) {
const actions = Object.assign({}, locationActions, formStateActions);
return {
actions: bindActionCreators(actions, dispatch)
};
}