我无法将我的动作作为道具发送(React 和 Redux)
I can't dispatch my actions as props (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.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()
不是函数的错误。这里实际上发生了什么?我该如何解决这个问题?
编辑:
这是日志数据:
换句话说,它只是在其周围添加 {}
。我试过单独使用 {formStateActions}
但它没有用,但 formStateActions
有效。
对于@LazarevAlexandr's,这是我的 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 很长,所以我不想 post 把它放在这里。它们都是函数,有些是 return 动作的 actioncreator,有些 return 是从 api.
中获取数据列表的函数
bindActionsCreators
只得到两个参数,所以如果想传递多个动作集试试这个:
function mapDispatchToProps(dispatch) {
const actions = Object.assign({}, locationActions, formStateActions);
return {
actions: bindActionCreators(actions, dispatch)
};
}
我是这样使用的 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()
不是函数的错误。这里实际上发生了什么?我该如何解决这个问题?
编辑:
这是日志数据:
换句话说,它只是在其周围添加 {}
。我试过单独使用 {formStateActions}
但它没有用,但 formStateActions
有效。
对于@LazarevAlexandr's,这是我的 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 很长,所以我不想 post 把它放在这里。它们都是函数,有些是 return 动作的 actioncreator,有些 return 是从 api.
中获取数据列表的函数bindActionsCreators
只得到两个参数,所以如果想传递多个动作集试试这个:
function mapDispatchToProps(dispatch) {
const actions = Object.assign({}, locationActions, formStateActions);
return {
actions: bindActionCreators(actions, dispatch)
};
}