首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何纠正Redux Reducer中的归一化实体数据

如何纠正Redux Reducer中的归一化实体数据
EN

Stack Overflow用户
提问于 2017-03-23 23:25:05
回答 1查看 125关注 0票数 1

全部:

我是相当新的还原减速器,大多数答案指向normalizr.js。例如:

代码语言:javascript
复制
const blogPosts = [
    {
        id : "post1",
        author : {username : "user1", name : "User 1"},
        body : "......",
        comments : [
            {
                id : "comment1",
                author : {username : "user2", name : "User 2"},
                comment : ".....",
            },
            {
                id : "comment2",
                author : {username : "user3", name : "User 3"},
                comment : ".....",
            }
        ]    
    },
    {
        id : "post2",
        author : {username : "user2", name : "User 2"},
        body : "......",
        comments : [
            {
                id : "comment3",
                author : {username : "user3", name : "User 3"},
                comment : ".....",
            },
            {
                id : "comment4",
                author : {username : "user1", name : "User 1"},
                comment : ".....",
            },
            {
                id : "comment5",
                author : {username : "user3", name : "User 3"},
                comment : ".....",
            }
        ]    
    }
    // and repeat many times
]

我将模式定义如下:

代码语言:javascript
复制
var authorSchm = new schema.Entity("authors", {}, {idAttribute: "username"});
var commentSchm = new schema.Entity("comments", {author:authorSchm})
var commentList = [commentSchm];
var postSchm = new schema.Entity("posts", {author:authorSchm, comments:commentList});
var postList = [postSchm];


var normalizedData = normalize(blogPosts, postList);
console.log(JSON.stringify(normalizedData, null, 4));

我得到的结果是:

代码语言:javascript
复制
{
    "entities": {
        "authors": {
            "user1": {
                "username": "user1",
                "name": "User 1"
            },
            "user2": {
                "username": "user2",
                "name": "User 2"
            },
            "user3": {
                "username": "user3",
                "name": "User 3"
            }
        },
        "comments": {
            "comment1": {
                "id": "comment1",
                "author": "user2",
                "comment": "....."
            },
            "comment2": {
                "id": "comment2",
                "author": "user3",
                "comment": "....."
            },
            "comment3": {
                "id": "comment3",
                "author": "user3",
                "comment": "....."
            },
            "comment4": {
                "id": "comment4",
                "author": "user1",
                "comment": "....."
            },
            "comment5": {
                "id": "comment5",
                "author": "user3",
                "comment": "....."
            }
        },
        "posts": {
            "post1": {
                "id": "post1",
                "author": "user1",
                "body": "......",
                "comments": [
                    "comment1",
                    "comment2"
                ]
            },
            "post2": {
                "id": "post2",
                "author": "user2",
                "body": "......",
                "comments": [
                    "comment3",
                    "comment4",
                    "comment5"
                ]
            }
        }
    },
    "result": [
        "post1",
        "post2"
    ]
}

我想知道如何在Redux减速器和combineReducers中使用这些规范化数据?我如何知道我应该根据引用字符串获取下一层数据的实体(例如:当我得到" post1“时,我如何知道我应该去posts实体获取post1对象,以及在post1对象中,如何对这些comment1,comment2进行处理。))

EN

回答 1

Stack Overflow用户

发布于 2017-03-23 23:43:01

对此的任何回答都将主要是主观的,并且取决于您的特定应用程序和用例。要获得通用和推荐的方法,请查看正式的示例。下面是combineReducers的样子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42988781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档