在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),
})
但它不起作用。我能做什么?我错过了什么吗?
错误是“无法读取属性”,然后是“未定义”。它发生了什么,一旦我输入代码,它应该创建一个组织并将我重定向到页面/仪表板,但它不起作用
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')
})
}
}
答案 0 :(得分:1)
根据您的代码,createOrganization
操作应为 async 。类似于:
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 }
希望它有意义。 Async
在redux中流动