【问题标题】:Handle POST request when using react-redux使用 react-redux 时处理 POST 请求
【发布时间】:2018-02-10 09:40:06
【问题描述】:

我有一个NewGroup 组件,它对后端进行 POST 调用。当调用返回时,相应地分派一个动作,并由reducer 更新应用程序状态。

但是!组件NewGroup 不知道新创建实体的 id 以在更新状态下查找它。

我应该如何处理这种情况?

一些代码:

这是减速器:

import { FETCH_GROUPS_SUCCESS, FETCH_GROUPS_ERROR, CREATE_GROUP_SUCCESS, CREATE_GROUP_ERROR, DELETE_GROUP, FETCH_GROUP_SUCCESS, FETCH_GROUP_ERROR } from '../actions/creators';

export const FULL_GROUP = 'full_group';
export const SNIPPET_GROUP = 'snippet_group';

const INITIAL_STATE = {
  data: null,
  isLoading: true,
  errorFetching: null
};

export default function(state=INITIAL_STATE, action) {
  switch(action.type) {
    case FETCH_GROUPS_SUCCESS:
      return {
        data: _.zipObject(_.map(action.payload, group => group.id),
          _.map(action.payload, group => ({
              data: group,
              isLoading: true,
              errorFetching: null,
              mode: SNIPPET_GROUP
          }))
        ),
        isLoading: false,
        errorFetching: null
      };
    case FETCH_GROUPS_ERROR:
      return {
        data: null,
        isLoading: false,
        errorFetching: action.payload.error
      };
    case CREATE_GROUP_SUCCESS:
      return { ...state, [action.payload.id]: {
        data: action.payload,
        isLoading: true,
        errorFetching: null,
        mode: SNIPPET_GROUP
      }};
    case CREATE_GROUP_ERROR:
      return { ...state, [action.payload.id]: {
        data: null,
        isLoading: false,
        errorFetching: action.payload.error
      }};
    default:
      return state;
  }
}

这是NewGroup 组件:

class NewGroup extends Component {

  _onSubmit(values) {
    this.props.createGroup(values);
  }

  render() {
    const { handleSubmit } = this.props;
    //id of the newly created/errored entity on the backend is unknown and there was it cannot be accessed here.
    return (
      <div>
        <form onSubmit={handleSubmit(this._onSubmit.bind(this))}>
          <Field name="name" label="Name" type="text" fieldType="input" component={renderField.bind(this)}/>
          <Field name="description" label="Description" type="text" fieldType="input" component={renderField.bind(this)}/>
          <button type="submit" className="btn btn-primary">Create</button>
          <button type="button" className="btn btn-primary" onClick={() => this.props.history.push('/group')}>Cancel</button>
        </form>
      </div>
    );
  }
}

export default reduxForm({
  validate,
  //a unique id for this form
  form:'NewGroup',
  validators
})(
  connect(null, { createGroup })(NewGroup)
);

这是redux-form 的事实在这里并不重要。

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    您可以将 id 存储在您的 redux 存储中(例如,newAddedSomethingId: 'yourID',一旦创建,在 POST 成功时,分派另一个操作(或者您甚至可以在同一个 reducer 中执行此操作,这取决于您如何拥有它设置)来存储 id 然后将其传递给组件。这样,一旦 redux 存储中的值从 POST 更新,您的组件将重新渲染,因此您的组件知道 ID。

    【讨论】:

    • 这实际上解决了问题,但它使应用程序状态结构看起来非常难看。我用一些代码更新了我的代码。也许有帮助。
    • 这不是关于丑陋,对我来说甚至 isLoading 都是丑陋的,但你需要它,你必须使用它。只需使用:lastCreatedGroup: [action.payload.id] 或类似的东西。您需要将该 ID 存储在某处,除非您按日期对它们进行排序,并获取最后添加的 ID,但如果其他人同时使用/更新您的数据,情况并非总是如此。
    • 这比使用回调更受欢迎吗?
    • 我已经与 Stephen Grider (github.com/StephenGrider)(Udemy 中不同 react/redux 课程的作者)讨论了关于存储在 redux 存储与回调中的问题,是的,它优于回调。跨度>
    • 我推荐看看redux sagas。使用 saga 模式可以很好地构建代码。
    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2018-05-12
    • 2019-04-05
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多