내 React 기반 프로젝트 (React Boilerplate에 빌드 됨)에 Redux와 Immutable.js를 사용하고 있으며, 변경하거나 변경 불가능한 것을 추가하는 관용적 인 방법을 찾고 있습니다. js 목록.immutable.js를 업데이트하거나 추가하는 방법
현재 설정. 국가는 처음에 다음과 같습니다
const initialState = fromJS({
accounts: [],
activeAccount: null,
loadedAccounts: [],
});
내가 account
개체에 대한 불변의 기록이 있습니다
const account = new Record({
description: '',
id: '',
name: '',
status: '',
});
그리고 내 감속기 :
function reducer(state = initialState, action) {
switch (action.type) {
case LOAD_ACCOUNT_SUCCESS:
return state
.set('activeAccount', new account(action.account))
default:
return state;
}
}
이 잘 작동 - LOAD_ACCOUNT_SUCCESS
조치가있을 때 해고되면 activeAccount
은 action.account
의 값으로 업데이트됩니다.
function reducer(state = initialState, action) {
switch (action.type) {
case LOAD_ACCOUNT_SUCCESS:
const loadedAccount = new account(action.account);
return state
.update('loadedAccounts', (loadedAccounts) => loadedAccounts.push(loadedAccount));
default:
return state;
}
}
을하지만, 새로운 레코드가 발생합니다 두 번 같은 계정 데이터를로드하는 순간에 밀려 : 모든 새로운 LOAD_ACCOUNT_SUCCESS
작업을 대신 loadedAccounts
에 새로로드 된 계정 데이터를 밀어 수 있도록
나는 이것을 수정할 수 있습니다 매번 내 목록에 올리십시오 (데이터 복제). 지금 내가 원하는 것은 입니다.action.account
을 loadedAccounts
에 추가하십시오 (일치하는 ID가있는 경우) 또는 목록의 레코드를 업데이트하십시오. 나는 유사한 질문과 슬픔에 잠긴 Immutable.js 문서를보고 있는데 어떻게 해야할지 모르겠다. 내가 기대했던대로 구문을 사용하지 않았다.
감사합니다. 잘못 입력하여 업데이트/추가 (사용자의 3 자 검사를 피하는)보다 우아한 방법이 있다고 가정했는데, 분명히 장점이 있다고 생각합니다. – CherryFlavourPez