TL;DR: If value is unexpectedly gone or undefined or null etc. go and see the async functions.
There are some questions about spread operator returning "undefined" but in my case I got absolutely nothing.
This is the process
[listof_objA, listof_objB]
.then(([listof_objA, listof_objB])=> { ...so on
dispatch(setListA(listof_objA))
likesomeAsyncFunc().then(([listof_objA, listof_objB])=> {
dispatch(setListA(listof_objA))
dispatch(setListB(listof_objB))
})
console.log(action.payload)
, it correctly displays the obtained listof_objA
case Types.SET_LISTA: {
console.log(action.payload)
return {
...state,
listof_objA: action.payload
}
}
case Types.SET_LISTA: {
console.log(action.payload)
return {
...state,
listof_objA: [...action.payload]
}
}
[]
. The action.payload
still correctly displays the obtained listof_objA
. JSON.parse(JSON.stringify(action.payload))
, const newList = [...action.payload]
Array(0)
but when dropped down it shows Array(13) [{...},{...}, ... , {...}]
.This is it. I couldn't find any solution. I don't know why this is happening. And it seems it happened only to me. Please help. If you need anymore information please comment. I am working with a company so I can't provide all information. But I'll try my best. Thank you.
I guess you are using a Promise in someAsyncFunc
, If so, the reason you get an empty array is because you probably didn't resolved your Promise chain correctly.
The correct Promise chain should look like this:
async function someAsyncFunc() {
return await fetch('ENDPOINT')
.then(response => {
return response.ok
? response.json()
: Promise.reject(Error('Unsuccessful response'));
})
.then(([listof_objA, listof_objB]) => {
dispatch(setListA(listof_objA));
dispatch(setListB(listof_objB));
});
}
In order to read the body of the responses JSON. You need to call the response.json() method. The json() method reads the full body of the response and parses the text as a JSON. Since this is another asynchronous operation it returns a promise itself.
Hope that it helps
@jank 回答我希望能解决你的问题,但你可能想看看redux-api-middleware作为一种更简单的 RPC 调用方式,没有所有的承诺。
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.