以下是normalizr库的github官方文档中的示例。我使用的是最新版本3.2.4
。
schema.js
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 ]
});
let originalData = {
"id": "123",
"author": {
"id": "1",
"name": "Paul"
},
"title": "My awesome blog post",
"comments": [
{
"id": "324",
"commenter": {
"id": "2",
"name": "Nicole"
}
}
]
}
const normalizedData = normalize(originalData, article);
console.log('NORMALIZED DATA: ', normalizedData)
,结果如下,
问题是未创建comment
和author
个实体。它们仍然嵌套在article
。
这是我的package.json
文件,
{
"dependencies": {
"@rails/webpacker": "^3.0.1",
"babel-preset-react": "^6.24.1",
"form-serialize": "^0.7.2",
"isomorphic-fetch": "^2.2.1",
"jquery": "^3.2.1",
"normalizr": "3.2.4",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-bootstrap": "^0.31.3",
"react-dom": "^15.6.1",
"react-redux": "^5.0.6",
"react-select-plus": "^1.0.0-rc.5",
"react-tooltip": "^3.3.1",
"redux": "3.5.2",
"redux-thunk": "^2.2.0",
"textile-js": "^2.0.4"
},
"devDependencies": {
"redux-logger": "^3.0.6",
"webpack-dev-server": "^2.7.1"
}
}
我花了很多时间找出没有运气的问题。看来,规范化与旧版本的库(Atleast,我用2.3.0
检查)一起工作正常。
有人可以帮我解决这个问题吗?设置有什么问题吗?或者我是否需要安装任何依赖项?