2017-12-15 40 views
0

を更新する方法JSの再来と反応は、次のように私は減速にReduxの状態を持っている状態に

[0] post_id:1010 
    post_title:"Title1" 
    post_comments [0] id=1 Title="Some text0" 
        [1] id=2 Title="Some text1" 

[1] post_id:1011 
    post_title:"Title2" 
    post_comments: [0] id=11 Title="Some text Comments0" 
        [1] id=22 Title="Some text Comments1" 

ユーザーが新しいポストのコメントを挿入すると、どのように私は私がidと応答性を持っている場合は、コメントを投稿する追加することができます= 33 Title = "Loreim Ispum"私はそれをpost_id:1011に追加する必要があります。 は、私は現在、減速中のように試してみました:

items = state.posts; 
    items[key].comments = items[key].comments.concat(action.data); 

    return Object.assign({}, state, { 
     asyncLoading: true, 
     asyncError: null, 
     posts:items 
    }); 

感謝を事前に

+0

のように広がり、オペレータとアップデートを利用することができ、配列のどの要素に、あなたそれをconcatする必要があります –

+0

私はpost_id:1011または投稿[1]キーの両方を取得できます。私はpost_commentsの2項目としてそれを追加する必要があります。 post_comments:[0]、ID = 11タイトル= "テキストの一部Comments0" [1]、ID = 22タイトル= "テキストの一部Comments1" \t \t [2] ID = 33タイトルは何= "Loreim Ispum" – jones

+0

'コメント'ここで' items [key] .comments.concat(action.data) '?それは 'post_comments'ですか? –

答えて

0

あなたはどのように決定されている

return { 
    ...state, 
     asyncLoading: true, 
     asyncError: null, 
     posts: { 
      ...state.posts.slice(0, post_index), 
      { 
       ...state.posts[post_index], 
       post_comments: [...state.posts[post_index].post_comments, action.payload] 
      }, 
      ...state.posts.slice(post_index 
+ 1), 
     } 
    } 
関連する問題