gpt4 book ai didi

javascript - 我无法将我的 Action 作为 Prop 发送(React 和 Redux)

转载 作者:行者123 更新时间:2023-11-29 21:21:34 26 4
gpt4 key购买 nike

下面是我如何使用 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>

这是要运行的函数:

stateToEntry() {
this.props.actions.stateToEntry();//formStateActions.stateToEntry();//dispatch an action to update the Redux store state
browserHistory.push('/location');//then redirect to the add/edit/delete page using browserHistory
}

我得到的错误是 this.props.actions.stateToEntry() 不是一个函数。这里实际上发生了什么?我该如何解决这个问题?

编辑:

这是日志数据: enter image description here

换句话说,它只是在其周围添加 {}。我试过单独使用 {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'};
}

我的 locationActions actioncreator 很长,所以我不想在这里完整发布它。它们都是函数,有些是返回 Action 的 actioncreator,有些返回从 api 获取数据列表的函数。

最佳答案

bindActionsCreators 只获取两个参数,所以如果想传递多个 Action 集试试这个:

function mapDispatchToProps(dispatch) {
const actions = Object.assign({}, locationActions, formStateActions);
return {
actions: bindActionCreators(actions, dispatch)
};
}

关于javascript - 我无法将我的 Action 作为 Prop 发送(React 和 Redux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38387715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com