2017-12-15 5 views
0

를 업데이트하는 방법 JS의 REDUX 반응다음과 같이 내가 감속기에 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 = "로렘 이력". 내가 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

처럼 확산 운영자 및 업데이트의 사용을 만들 수, 배열의 어떤 요소에, 당신 연결해야합니다 –

+0

나는 post_id : 1011 또는 post [1] 즉 키를 모두 얻을 수 있습니다. 난 그냥 post_comments에 2 항목으로 추가해야합니다. post_comments [0] 식 11 명 = "텍스트 Comments0" [1] 식 (22) 제목 = "텍스트 Comments1" \t \t [2] 식 (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), 
     } 
    }