Reactjs 如何在redux中映射合并的规范化api响应

Reactjs 如何在redux中映射合并的规范化api响应,reactjs,redux,react-redux,normalizr,Reactjs,Redux,React Redux,Normalizr,最近开始对API响应进行规范化,以使其更加平坦。 我已经成功地从这个 为此: case FETCH_IMAGES_SUCCESS: console.log(action.images.entities) 如何迭代它 以前我使用this.props.images.map()但是现在数据是分开的 我应该如何使用相应的嵌套数据迭代images数组 Reducer.js import { UPLOAD_IMAGE_SUCCESS, POST_COMMENT_SUCCESS,

最近开始对API响应进行规范化,以使其更加平坦。 我已经成功地从这个

为此:

 case FETCH_IMAGES_SUCCESS:
      console.log(action.images.entities)

如何迭代它

以前我使用
this.props.images.map()
但是现在数据是分开的

我应该如何使用相应的嵌套数据迭代images数组

Reducer.js

import {
  UPLOAD_IMAGE_SUCCESS,
  POST_COMMENT_SUCCESS,
  DELETE_IMAGE_FAILURE,
  FETCH_IMAGES_SUCCESS,
  POST_COMMENT,
  POST_LIKE,
  POST_LIKE_SUCCESS,
  DISLIKE_POST_SUCCESS,
  DELETE_IMAGE_SUCCESS,
} from '../types';
import { REHYDRATE, PURGE, FLUSH }from 'redux-persist'
// We use seamless-immutable but thats for
const initialState = {
  images: [],
  likeCount: [],
  liked: false
};
export default (state = initialState, action) => {
  switch (action.type) {
    case FETCH_IMAGES_SUCCESS:
      console.log(action.images.entities)
      // return {
      //   ...state,
      //   images: action.images.entities.images,
      //   ...state.images
      // };

    default:
      return state;
  }
};
import { normalize, schema } from "normalizr";
import {imageListSchema} from "../schemas";

export function* getImages() {
  try {
    const images = yield call(api.images.fetchImages);
    // debugger;
    // console.log(normalize(images, [imageSchema]));
    // console.log(images)
    const data = normalize(images, imageListSchema )
    yield put(fetchImagesSuccess(data));

  } catch (error) {
    yield put(fetchImageFailure(error.response));
  }
}
saga.js

import {
  UPLOAD_IMAGE_SUCCESS,
  POST_COMMENT_SUCCESS,
  DELETE_IMAGE_FAILURE,
  FETCH_IMAGES_SUCCESS,
  POST_COMMENT,
  POST_LIKE,
  POST_LIKE_SUCCESS,
  DISLIKE_POST_SUCCESS,
  DELETE_IMAGE_SUCCESS,
} from '../types';
import { REHYDRATE, PURGE, FLUSH }from 'redux-persist'
// We use seamless-immutable but thats for
const initialState = {
  images: [],
  likeCount: [],
  liked: false
};
export default (state = initialState, action) => {
  switch (action.type) {
    case FETCH_IMAGES_SUCCESS:
      console.log(action.images.entities)
      // return {
      //   ...state,
      //   images: action.images.entities.images,
      //   ...state.images
      // };

    default:
      return state;
  }
};
import { normalize, schema } from "normalizr";
import {imageListSchema} from "../schemas";

export function* getImages() {
  try {
    const images = yield call(api.images.fetchImages);
    // debugger;
    // console.log(normalize(images, [imageSchema]));
    // console.log(images)
    const data = normalize(images, imageListSchema )
    yield put(fetchImagesSuccess(data));

  } catch (error) {
    yield put(fetchImageFailure(error.response));
  }
}
您可以使用在前端处理此类关系规范化数据

这涉及到将后端表转换为模型,然后通过外键在它们之间建立关系。如果您曾经在关系数据库中工作过,那么它将在react中对相同的原则进行密切的建模

import { Model, fk, many, attr } from 'redux-orm';

class Comment extends Model {//static or instances go here}
Comment.modelName = 'Comment';

// Declare your related fields.
Comment.fields = {
    id: attr(),
    name: attr(),
    userId: fk(//declare foreign key to User model here),
};
设置好所有模型后,您可以通过其API进行查询、更新和删除,如下所示:

Comment.create(response);
Comment.all();
Comment.withId(1).update({ data: 'Nice Book!' });
Comment.all().filter(comment => comment.userId === '123').first().delete();
PS:我还没有在生产中使用它(切换到GraphQL)。如果我下次有机会的话,我想尝试一下,你可以用它在前端处理这样的关系规范化数据

这涉及到将后端表转换为模型,然后通过外键在它们之间建立关系。如果您曾经在关系数据库中工作过,那么它将在react中对相同的原则进行密切的建模

import { Model, fk, many, attr } from 'redux-orm';

class Comment extends Model {//static or instances go here}
Comment.modelName = 'Comment';

// Declare your related fields.
Comment.fields = {
    id: attr(),
    name: attr(),
    userId: fk(//declare foreign key to User model here),
};
设置好所有模型后,您可以通过其API进行查询、更新和删除,如下所示:

Comment.create(response);
Comment.all();
Comment.withId(1).update({ data: 'Nice Book!' });
Comment.all().filter(comment => comment.userId === '123').first().delete();

PS:我还没有在生产中使用它(切换到GraphQL)。如果下次有机会的话,我想尝试一下这一点

Object.keys()
Object.values()
?它似乎不一致。这似乎有点骇人听闻。。我在想类似的东西,比如postsById,等等。但不知道如何处理这个问题。
Object.keys()
Object.values()
?它似乎不对齐。这似乎有点骇人听闻。。我一直在想这样的事情,比如postsById,等等。不过我不知道该怎么做。谢谢分享,我会记住这个答案,但这不是我想要的方法。我想我会尝试像这样把减速机分离出来谢谢分享,会记住这个答案,但这不是我想要的方法。我想我得试着像这样把减速机分开