2017-12-15 7 views
0

ich einen redux Zustand in Minderer haben wie folgt: Beiträge:Reagieren js redux wie Zustand aktualisieren

[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" 

Wenn der Benutzer einen neuen Beitrag Kommentare einfügt, wie kann ich hinzufügen, um Kommentare zu schreiben, wenn ich Antwort als id haben = 33 Titel = "Loreim Ispum". Ich muss es zu post_id hinzufügen: 1011 was ich auch als Antwort habe. Ich habe versucht, zur Zeit wie in Minderer:

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

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

Vielen Dank im Voraus

+0

machen könnten Wie Bestimmen Sie, in Array, dem Element, Sie muss es concat –

+0

Ich kann sowohl post_id: 1011 oder post [1] dh Schlüssel. Ich muss es nur als 2 Artikel in den post_comments anhängen. post_comments: [0] id = 11 Title = "Ein Text Kommentare0" [1] id = 22 Title = "Ein Text Kommentare1" \t \t [2] id = 33 title = "Loreim Ispum" – jones

+0

Was 'Kommentare ist 'here' Elemente [Schlüssel] .comments.concat (action.data)'? Ist das 'post_comments'? –

Antwort

0

Sie Verwendung von Spread-Operator und Update wie

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), 
     } 
    } 
Verwandte Themen