以下是我的初始默认状态值
export const parameterInitialState = {
id: '',
name: '',
parameterSettings: parameterSettingsInitialState,
parameterMediaSettings: parameterMediaSettingsInitialState,
};
export const parameterSettingsInitialState = {
attributeId: '',
value: '',
};
export const parameterMediaSettingsInitialState = {
barcodeSupported: false,
mediaTypeId: '',
qrcodeSupported: false,
scratchOffPINSupported: false,
};
在parameterState reducer中,我使用上面的parameterInitialState
const initialState = {
parameterSettings: parameterInitialState,
};
export default function parameterState( state = initialState, action ) {
switch ( action.type ) {
case FETCH_PARAMETER_SUCCESS:{
return { ...state, ...action.payload };
}
default:
return state;
}
}
但我的问题是在初始状态下,对象parameterSettings和parameterMediaSettings变得未定义
答案 0 :(得分:2)
更改初始状态变量的定义顺序,在给定的顺序变量parameterSettingsInitialState
和parameterMediaSettingsInitialState
将返回undefined。
因此,请确保首先定义子对象变量,然后定义您计划包含在其中的父对象。
export const parameterSettingsInitialState = {
attributeId: '',
value: '',
};
export const parameterMediaSettingsInitialState = {
barcodeSupported: false,
mediaTypeId: '',
qrcodeSupported: false,
scratchOffPINSupported: false,
};
export const parameterInitialState = {
id: '',
name: '',
parameterSettings: parameterSettingsInitialState,
parameterMediaSettings: parameterMediaSettingsInitialState,
};
更新了parameterSettingsInitialState
和parameterMediaSettingsInitialState
作为数组的答案:
export const parameterSettingsInitialState = [{
attributeId: '',
value: '',
}];
export const parameterMediaSettingsInitialState = [{
barcodeSupported: false,
mediaTypeId: '',
qrcodeSupported: false,
scratchOffPINSupported: false,
}];
export const parameterInitialState = {
id: '',
name: '',
parameterSettings: parameterSettingsInitialState,
parameterMediaSettings: parameterMediaSettingsInitialState,
};