我有一個profilesReducer,我想用它在我的redux商店中存儲一個或多個用戶配置文件。作爲一個例子,認爲Twitter需要存儲我的個人資料以及其他個人資料。React Redux reducer覆蓋值LOAD_SUCCESS?
這是我的profilesReducer.js:
import * as types from '../actions/actionTypes';
const initialState = []
export default function profilesReducer(state = initialState, action) {
switch (action.type) {
case types.LOAD_PROFILE_SUCCESS:
return [Object.assign({}, action.profile)]
case types.UPDATE_PROFILE_SUCCESS:
return [
...state.filter(profile => profile.id !== action.profile.id),
Object.assign({}, action.profile)
]
default:
return state;
}
}
問題是負載爲接收多個配置文件(由profile.id區分),但overwritting在商店中的現有簡檔,而不是追加/更新。
我需要LOAD_PROFILE以允許商店中的多個用戶個人資料。有什麼建議麼?由於
爲什麼使用傳播的Object.assign在一起的時候只有一個能爲你做的工作 –
@ShubhamKhatri,我傻我已經更新了它。 – Purgatory