我正在尝试使用传播对象更新对象,但无法正确更新它。 这是我在第一个开关案例中想要的结果:
{
code: {
loading: true
}
}
在第二种情况下,这就是我想要的:
{
code : {
loading: false,
data: {
some data here
}
}
}
const downloadData = (state = {}, action) => {
switch (action.type) {
case LOADING: {
const {code, loading} = action.payload
return {
...state,
[code]: {
...[code], loading
}
}
}
case LOADED: {
const {code, loading, data} = action.payload
return {
...state,
[code] : {
loading, data, ...[code]
}
}
}
default:
return state
}
}
答案 0 :(得分:0)
我认为您的第二种情况应该是:
case LOADED: {
const {code, loading, data} = action.payload
return {
...state,
code : {
...code,
loading:false,
data: {some data here}
}
}
}
因为loading
和data
将覆盖...[code]
答案 1 :(得分:0)
假设您具有初始状态
const initialState = {
code: {
loading: false,
data: {}
}
};
对于第一种情况,请尝试:
{
...state,
code: {
...state.code,
loading: true
}
}
第二种情况:
{
...state,
code: {
...state.code,
loading: false,
data: {
some data here
}
}
}