I am trying to update object using spread object, but not able to correctly update it. This is the result I want in first switch case:
{
code: {
loading: true
}
}
And in second switch case, this is what I want:
{
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
}
}
I think your second case should be:
case LOADED: {
const {code, loading, data} = action.payload
return {
...state,
code : {
...code,
loading:false,
data: {some data here}
}
}
}
Because the loading
and data
will overwrite the ...[code]
Assuming that you have initial state
const initialState = {
code: {
loading: false,
data: {}
}
};
For the first case try:
{
...state,
code: {
...state.code,
loading: true
}
}
For second case:
{
...state,
code: {
...state.code,
loading: false,
data: {
some data here
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.