我是使用 React-Redux 的新手。因此,我在这种特殊情况下面临一些问题。我的 Redux 商店是这样的:
{
info: {firstName: 'abc', lastName: 'xyz', email: abc.xyz@gmail.com, name: 'abc xyz', id: 145},
userInfo: {firstName: 'abc', lastName: 'xyz', email: abc.xyz@gmail.com, contactNumber: 1234567890, country: 'India'}
}
现在,我从不同的页面收到这两个详细信息。我将它们存储在商店中,并使用 react-redux 进行 API 调用以更新商店数据。因此,我使用单独的 API 调用接收这两个数据。现在,当我更新 userInfo 有效负载时,我可以成功更新我的 userInfo 存储状态。但是,firstName 和 lastName 是两个常见的字段。因此,每次我更新 userInfo 时,更新信息的最佳做法应该是什么,因为它们必须同步且相同。
粘贴一些redux代码以供参考:
reducer.js(供参考)
import * as actionTypes from './actionTypes';
export const initialState = {
email: null,
firstName: null,
id: null,
lastName: null,
name: null
};
export default function(state = initialState, action) {
switch (action.type) {
case actionTypes.SET_INFORMATION: {
const { info = {} } = action;
return { ...info };
}
default:
return state;
}
}
reducer.js(用于用户信息)
import * as actionTypes from './actionTypes';
export const initialState = {
firstName: null,
lastName: null,
email: null,
contactNumber: null,
country: null
};
export default function(state = initialState, action) {
switch (action.type) {
case actionTypes.USER_INFORMATION: {
const { userInfo = {} } = action;
return { ...state, ...userInfo };
}
case actionTypes.GET_USER_INFORMATION_ERROR:
case actionTypes.PATCH_USER_INFORMATION_HEADER_ERROR: {
const { error = {} } = action;
return { ...error };
}
case actionTypes.PATCH_USER_INFORMATION_HEADER: {
const { userInfo: { data = {} } = {} } = action;
return { ...state, ...data };
}
default:
return state;
}
}
请帮助我采用最佳做法,以便在更新另一个数据时更新这两个数据以保持数据同步。TIA。