问题
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