我正在研究React / Redux电子商务项目,我正在制作添加用户地址的功能,功能正常,我能够添加新的用户地址,但问题是我必须刷新页面到显示新的地址卡,地址卡保存后没有显示地址,我要刷新页面显示新地址,任何人都可以帮忙解决这个问题,我的动作,reducer和saga文件附在下面
操作文件
const addAddressByBuyerId = (address: Object, params: string) => ({
type: Types.ADD_ADDRESS_BY_BUYER_ID,
address,
params,
});
const addAddressByBuyerIdSuccess = (addresses: Object, params: string) => ({
type: Types.ADD_ADDRESS_BY_BUYER_ID_SUCCESS,
addresses,
params,
});
const addAddressByBuyerIdFailure = (errorMessage: Object) => ({
type: Types.ADD_ADDRESS_BY_BUYER_ID_FAILURE,
errorMessage,
});
传奇文件
function* addAddressByBuyerId(action) {
try {
const response = yield call(api.account.postAddressByBuyerId, action.address);
if (response.ok && response.data && {}.hasOwnProperty.call(response.data, 'data')) {
yield put(Actions.getAddressesByBuyerId());
yield put(Actions.addAddressByBuyerIdSuccess(response.data.data));
} else {
yield put(Actions.addAddressByBuyerIdFailure(response.errorMessage));
}
} catch (error) {
yield put(Actions.addAddressByBuyerIdFailure());
}
}
function* watchAddAddressByBuyerId(): Generator<void, void, void> {
yield takeLatest(Types.ADD_ADDRESS_BY_BUYER_ID, addAddressByBuyerId);
}
减少文件
export const addAddressByBuyerId = (state: Object = INITIAL_STATE) => ({
...state,
isLoading: true,
successMessage: null,
errorMessage: null,
});
export const addAddressByBuyerIdSuccess = (state: Object = INITIAL_STATE, action: Object) => {
const successMessage = I18n.getText('account.address-added', {}, 'Address added!');
return {
...state,
address: action.address,
isLoading: false,
successMessage,
};
};
export const addAddressByBuyerIdFailure = (state: Object = INITIAL_STATE, action: Object) => ({
...state,
errorMessage: action.errorMessage || genericError,
isLoading: false,
});
答案 0 :(得分:0)
你有一个错字。
在操作文件中,您返回包含addresses
属性的对象(请参阅addAddressByBuyerIdSuccess
函数)。
但是在您的reducer(addAddressByBuyerIdSuccess
)中,您正在访问未定义的address
属性。