normalizr 介绍
normalizr可以将嵌套的jsON格式扁平化,方便被Redux利用;
normalizr主要将获取的数据进一步格式化,在store创建一个虚拟数据库,数据通过id引用。
normalizr安装
yarn add normalizr
npm install normalizr
normalizr使用
没有normaliz 的情况:
{
"id": "123",
"author": {
"id": "1",
"name": "Paul"
},
"title": "My awesome blog post",
"comments": [
{
"id": "324",
"commenter": {
"id": "2",
"name": "Nicole"
}
}
]
}
引入normalizr代码:
import { normalize,schema } from 'normalizr';
// Define a users schema
const user = new schema.Entity('users');
// Define your comments schema
const comment = new schema.Entity('comments',{
commenter: user
});
// Define your article
const article = new schema.Entity('articles',{
author: user,
comments: [comment]
});
const normalizedData = normalize(originalData,article);
结果如下:
{
result: "123",
entities: {
"articles": {
"123": {
id: "123",
author: "1",
title: "My awesome blog post",
comments: [ "324" ]
}
},
"users": {
"1": { "id": "1","name": "Paul" },
"2": { "id": "2","name": "Nicole" }
},
"comments": {
"324": { id: "324","commenter": "2" }
}
}
}
网站地址:https://github.com/paularmstrong/normalizr
GitHub:https://github.com/paularmstrong/normalizr
网站描述:一款JSON数据范式化的js库
normalizr
官方网站:https://github.com/paularmstrong/normalizr
小编说
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。