T
TanStack2y ago
fair-rose

V5 Infinite loop issue

Alright so the old code is like
const [data1, setData1] = useState()
const [data2, setData2] = useState()
const [data1, setData1] = useState()
const [data2, setData2] = useState()
When I get data from useQuery, then onSuccess, I kinda assign data.data1 to data1 state, and data.data2 to data2 state, then I use those states to do other things. So far so good.... then upon upgrading to V5, I found this one is not working well as it will cause "too many re-render". That is I want to do the same when doing
if (isSuccess){
setData1(data.data1);
setData2(data.data2);
}
if (isSuccess){
setData1(data.data1);
setData2(data.data2);
}
How do I deal with such issues? Sometimes we can't just directly use the data we fetch from useQuery, as we might get different versions, and they have different structure, so I might need setData1 (data.oldVersion.data1) if it's an old version etc....
2 Replies
fair-rose
fair-roseOP2y ago
I found a way to solve this by wrapping isSuccess block within a useEffect hook.
frozen-sapphire
frozen-sapphire2y ago
Just don’t do it like that. It‘s very wrong

Did you find this page helpful?