2017-03-23 6 views
1

すべて:Redux Reducerで規格化されたエンティティデータを参照する方法

私はredux reduderにはかなり新しく、ほとんどの回答はnormalizr.jsを指しています。私は以下のようにスキーマを定義

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 
] 

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)); 

そして、私は次のようになります:のような例に取ると、私はReduxのリデューサに、この正規化されたデータを使用しない方法を疑問に思う

{ 
    "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" 
    ] 
} 

とcombineReducers? そして参照文字列に従って次のレベルのデータを取得するためにどのエンティティを取得する必要があるかを知るにはどうすればよいですか(たとえば、「post1」を取得すると、投稿エンティティに行き、投稿1オブジェクト、それらのコメント1、コメント2のためにそれを行う方法)

答えて

0

これに対する答えは主にあなたの特定のアプリケーションとユースケースによって異なります。一般的で推奨される方法については、exampleの公式をご覧ください。ここにはcombineReducersの外観があります。

+0

ありがとう、私のblogpostsの例によると、あなたは主観的なことができるようにビットの詳細を話すことができますか? – Kuan

関連する問題