我刚开始在Redux中使用normalizr,但我陷入了一个简单的问题,但我做错了。所以我想规范化这样的数组:
{
articles: [
{id: 1, someValue: 'test'},
{id: 2, someValue: 'test2'}
]
}
Run Code Online (Sandbox Code Playgroud)
变成这样的结构:
{
result: {
articles: [1,2]
},
entities: {
articles: {
1: {someValue: 'test'},
2: {someValue: 'test2'}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试这样做:
const article = new Schema('articles');
responce = normalize(responce, {
articles: arrayOf(article)
});
Run Code Online (Sandbox Code Playgroud)
但这给了我一个看起来像这样的结构:
{
articles: {
entities: {},
result: {
0: {someValue: 'test'},
1: {someValue: 'test2'}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在没有文章ID的数组。我假设我在这里缺少什么:
article.define({
...
});
Run Code Online (Sandbox Code Playgroud)
但是在这种简单的情况下无法确定需要去哪里
您不必定义文章。确保已normalizr正确导入所有内容。我尝试了您的代码,它给了我预期的结果:
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);
Run Code Online (Sandbox Code Playgroud)
输出:
{
result: {
articles: [1,2]
},
entities: {
articles: {
1: {someValue: 'test'},
2: {someValue: 'test2'}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4196 次 |
| 最近记录: |