reactjs - 如何在我的减速器功能中 console.log state.car.price
问题描述
我似乎无法访问嵌套在对象中的属性。我想让我的应用程序启动并运行,但我无法通过这个错误。
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)
};
解决方案
您在执行 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。
推荐阅读
- scala - Scala/Akka 拉模式 - 经理从工人那里获得更多工作
- maven - 当依赖关系树中存在两个或多个依赖关系版本时,Maven 如何选择传递依赖关系的版本?
- python-3.x - Python 连接到 EMR 上的 Hive
- jenkins - 为多个可选阶段运行并行性
- shell - 无法为“协议”找到合适的输出格式
- angular - 在 Angular 4 的组件中捕获 ngValue 属性中的值
- .net - 通过公共函数 (.NET) 在 WITH Trough a Public function (.NET) 中引用 FORM 和控件
- sphinx - 如何从 Sphinx 源配置中删除继承的值?
- winapi - WinApi:使用 VirtualAlloc 跨两个进程共享内存
- python - 合并两个大熊猫数据框