我似乎无法访问嵌套在对象内的属性。我想启动并运行我的应用程序,但无法解决此错误。
const initialState = {
additionalPrice: 0,
title:"hi",
car: {
price: 26395,
name: '2019 Ford Mustang',
image:
'https://cdn.motor1.com/images/mgl/0AN2V/s1/2019-ford-mustang-bullitt.jpg',
features: []
},
additionalFeatures: [
{ id: 1, name: 'V-6 engine', price: 1500 },
{ id: 2, name: 'Racing detail package', price: 1500 },
{ id: 3, name: 'Premium sound system', price: 500 },
{ id: 4, name: 'Rear spoiler', price: 250 }
]
};
export const featuresReducer = (state = initialState, action) => {
console.log(action, state);
switch (action.type) {
case ADD_FEATURE:
return {
...state,
...state.car:{...state.car:price:5}
}
default:
return state;
}
console.log(state.car.price)
};
答案 0 :(得分:0)
您如何称呼FeatureReducer?如果仅传递一个参数,则将其放入state
变量中;如果传递两个参数,则它将覆盖initialState
默认值。
答案 1 :(得分:0)
您将在执行console.log之前返回switch语句。另外,我认为您在使用“,”的地方有一个“:”。尝试这样的事情:
export const featuresReducer = (state = initialState, action) => {
console.log(action, state);
switch (action.type) {
case ADD_FEATURE:
console.log("here");
return {
...state,
car: { ...state.car, price: 5 }
}
default:
return state;
}
};
如果要查看state.car.price,必须在执行reducer操作之后调用console.log。