2017-10-24 5 views
0

나는 redux를 배우고 있으며 상태에서 항목 하나를 삭제하는 방법을 궁금해하고 있습니다.Redux의 배열에서 항목 삭제

export const getInitialState =() => { 
    let state = { 
    isLogged: false, 
    organizations: [], 
    userData: {}, 
    activeIndex: -1, 
    currentRetrospective: {}, 
    hasFetched: false 
    } 

데이터가 이제 내부 organizations

case `${actions.ACTION_GET_USER_ORGS}_FULFILLED`: { 
    let activeIndex = 0 
    if (state.activeIndex !== -1) { 
     activeIndex = state.activeIndex 
    } else if (action.payload.data.length === 0) { 
     activeIndex = -1 
    } 
    return { ...state, activeIndex, organizations: action.payload.data, hasFetched: true } 
    } 

을 사는 방법이 있습니다, 제가해야 할 것은 organizationretrospectives 배열에서 하나 개의 항목을 삭제하는 것입니다 :이 초기 상태를 가지고있다. 나는 이것을 시도했지만 효과가 없다. 그것을 할 수있는 더 좋은 방법이 있습니까?

export default (state = getInitialState(), action) => { 
    switch (action.type) { 

    case `${actions.ACTION_DELETE_RETROSPECTIVE}_FULFILLED`: { 

    const { organizations, activeIndex } = state 

    const newOrganizations = JSON.parse(JSON.stringify(organizations)) 

    const activeOrganization = newOrganizations[activeIndex] 

    activeOrganization.retrospectives = activeOrganization.retrospectives 
     .filter((retro) => retro.id != action.retroId) 

    return { ...state, organizations: newOrganizations } 
    } 

고마워요!

+0

어떻게 데이터가 조직 배열 당신의 내부에 살고을? –

+0

당신의 코멘트와 당신의 대답은 여전히 ​​작동하지 않습니다 : ( –

+0

왜이? JSON.parse (JSON.stringify (organizations)) – bitstrider

답변

1

이 같은 조직 배열 필터링 할 수 있습니다

export default (state = getInitialState(), action) => { 
     switch (action.type) { 
      case `${actions.ACTION_DELETE_RETROSPECTIVE}_FULFILLED`: { 
       return { 
        ...state, 
        organizations: state.organization.filter(retro => 
         retro.id !== action.retroId } 
     }