为什么需要 normalizr
- 有时后端的接口(更多时候是已经不维护的旧接口)存在多层或者嵌套,前端需要转换数据
- 前端需要分不同维度去取数据
- redux 中 state 的结构有多层,而前端只想取其中一层
- react 中的 PropsType
实例及解析过程
已经有一个成熟的范式化库normalizr
// originData
{
"id": "123",
"author": {
"id": "1",
"name": "Paul"
},
"title": "My awesome blog post",
"comments": [
{
"id": "324",
"commenter": {
"id": "2",
"name": "Nicole"
}
}
]
}
// 范式化过程,请按照步骤查看思路
import { normalize, schema } from 'normalizr';
// Define a users schema
// 步骤三,因为user是一个users schema实例,所以相当于该user对应着{"id": "1","name": "Paul"},默认序