mapDispatchToProps是显式的

时间:2017-12-27 16:34:29

标签: reactjs redux

在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')
      })
    }
  }

1 个答案:

答案 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中流动