在react/redux中,我尝试将此mapDispatchToProps转换为显式:
const mapDispatchToProps = { createOrganization }
我试过这个:
const mapDispatchToProps = (dispatch) => {
return {
createOrganization: (organization) => {
dispatch(createOrganization(organization))
}
}
}这就是行动
export const createOrganization = (organization) => ({
type: ACTION_CREATE_ORGANIZATION,
payload: api.createOrganization(organization),
})但它也不起作用。我能做什么?我是不是遗漏了什么?
错误是“无法读取未定义的属性'then‘”。实际情况是,一旦我输入代码,它就会创建一个组织,并将我重定向到页面/dashboard,但它不起作用
handleClick = (e, formData) => {
e.preventDefault()
if (formData.betaCode && formData.organization && this.props.userData) {
this.props.createOrganization({
name: formData.organization,
owner: {
id: this.props.userData.id,
name: this.props.userData.login,
ownerAvatar: this.props.userData.avatar_url
},
beta_code: formData.betaCode
})
.then(() => {
this.props.history.push('/dashboard')
})
}
}发布于 2017-12-28 02:57:26
根据您的代码,createOrganization操作应该是异步的。类似于:
const = createOrganization = organization => dispatch =>
api.createOrganization(organization)
.then(
response => dispatch({ type: ACTION_CREATE_ORGANIZATION, payload: response}),
error => dispatch({ type: ACTION_CREATE_ORGANIZATION_ERROR, payload: error}),
)但这还不够,您应该安装redux-thunk && redux-promise来处理此类操作。
代码的其余部分不应该被更改。然后,您将能够随心所欲地使用mapDispatchToProps:
const mapDispatchToProps = { createOrganization }
希望这是有意义的。redux中的Async流
https://stackoverflow.com/questions/47995582
复制相似问题