다음 json 파일을 감속기의 클라이언트 상태로 저장했습니다. 이제 아약스를 통해 새로운 노트를 추가 할 때 기존 메모 배열에 새 노트를 추가하는 NEW_NOTE_SUCCESS 리듀서를 호출합니다. 내가 위를 한 후Redux Reducer - 중첩 된 상태에 새 데이터 추가
export default function clientProfile(state={}, action) {
switch(action.type) {
case NEW_NOTE_SUCCESS:
console.log(action.payload);
return { ...state, notes: [...state.notes, action.payload] }
default:
return state;
}
}
그러나, this.props.client.notes은 새로운 I 추가 참고 있지만 오래된 사람이있을 것이다.
새 메모를 배열에 "푸시"해야 할 경우, 새 메모를 추가 할 때마다 메모 json 파일을 다시 보내지 않아도됩니다.
PS : action.payload에는 다음 JSON이 포함되어 있습니다.
{
"noteId": "10000000",
"customerId": "34",
"createdBy": "1",
"created": "1510316194",
"note": "ADDED A NEW NOTE. NEED TO ADD TO STATE"
}
이것은 초기로드의 원래 json 파일입니다.
{
"customerId": "34",
"agentId": "1",
"createdDate": "1510223892",
"firstname": "John",
"lastname": "Doe",
"mobile": "123456789",
"address": "Something email here",
"notes": {
"0": {
"noteId": "1",
"customerId": "34",
"createdBy": "1",
"created": "1510316194",
"note": "add something"
},
"1": {
"noteId": "2",
"customerId": "34",
"createdBy": "1",
"created": "1510316527",
"note": "add something"
},
"2": {
"noteId": "3",
"customerId": "34",
"createdBy": "1",
"created": "1510317177",
"note": "add new thing"
},
"3": {
"noteId": "4",
"customerId": "34",
"createdBy": "1",
"created": "1510318448",
"note": "something"
},
"4": {
"noteId": "5",
"customerId": "34",
"createdBy": "1",
"created": "1510318476",
"note": "this would be note number 5, lets see whether we can get something from this."
}
}
}
업데이트 : Reducer Codes - Array 대신 Reducer Codes - Object.
case NEW_NOTE_COMPLETE:
return { ...state, notes: {...state.notes, action.payload.noteId: action.payload} }
어떤 일이 일어날한다 : 여기
작은 실행 예입니다? 그것을 재정의하거나 새 메모의 키를 변경해야합니까? –
noteId 만 유일하므로 배열 키 5를 예로 넣는 것입니다. –
흠 나는 혼합 된 데이터 구조를 가지고 있다고 생각합니다. 당신은 당신의 json 파일에서'notes'가 객체이지만 여러분의 감속기에서는 배열임을 알 수 있습니다. 나는이 줄'note : {... state.notes, "someKey": action.payload} 대신에''notes :: [... state.notes, action.payload]' –