I'm just getting started with using normalizr with Redux and I'm stuck on what seems to me like a simple problem but I could be doing this wrong. So I want to normalize an array like this:
{
articles: [
{id: 1, someValue: 'test'},
{id: 2, someValue: 'test2'}
]
}
into a structure like this:
{
result: {
articles: [1,2]
},
entities: {
articles: {
1: {someValue: 'test'},
2: {someValue: 'test2'}
}
}
}
I have tried doing this:
const article = new Schema('articles');
responce = normalize(responce, {
articles: arrayOf(article)
});
But that gives me a structure that looks like this:
{
articles: {
entities: {},
result: {
0: {someValue: 'test'},
1: {someValue: 'test2'}
}
}
}
which now has no array of article ids. I am assuming I am missing something here:
article.define({
...
});
but can't figure what needs to go there in this simple case
You don't have to define article. Make sure you've imported everything from normalizr
correctly. I tried your code and it gave me the expected result:
import { normalize, Schema } from 'normalizr';
let response = {
articles: [
{ id: 1, someValue: 'test' },
{ id: 2, someValue: 'test2' }
]
};
const article = new Schema('articles');
response = normalize(response, {
articles: [article]
});
console.log(response);
Output:
{
result: {
articles: [1,2]
},
entities: {
articles: {
1: {someValue: 'test'},
2: {someValue: 'test2'}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With