how to denormalize entities from redux store?

点点圈 提交于 2021-01-28 19:08:16

问题


i have a data state in redux store like:

{
  123: { a: 'abc' },
  456: { a: 'def' },
  789: { a: 'ghi' },
}

this is an array of objects like:

const trainersArray= [ { _id: 123, a: 'abc' }, { _id: 456, a: 'def' }, { _id: 789, a: 'ghi' }, ]

  export const trainer = new schema.Entity(
   'trainers',
   {},
   { idAttribute: '_id' }
  );

  const { trainers: normalizedTrainers } = normalize(trainersArray, [
    trainer,
  ]).entities;

now i want to denormalize like below in a component but i just don't get it?

const normalizedTrainers = useSelector(({ trainers}) => trainers);
const denormalizedData = denormalize(Object.keys(normalizedTrainers), [trainer], normalizedTrainers );

i just don't understand the example from docs :(

import { denormalize, schema } from 'normalizr';

const user = new schema.Entity('users');
const mySchema = { users: [user] };
const entities = { users: { '1': { id: 1 }, '2': { id: 2 } } };
const denormalizedData = denormalize({ users: [1, 2] }, mySchema, entities);

how do i do it like mine?

来源:https://stackoverflow.com/questions/61741464/how-to-denormalize-entities-from-redux-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!