Infinite useEffect cycle in answer section
In answer-sections.tsx
there's a useEffect statement as following.
useEffect(() => {
if (data?.has_answers !== has_answers && !has_answers && data) {
setData({
...data,
answers: [],
allow_new_answer: true,
allow_new_legacy_answer: true,
});
}
if (
(data === undefined || data.cutVersion !== cutVersion) &&
(visible || !hidden)
) {
run();
}
}, [data, visible, run, cutVersion, hidden, has_answers]);
This calls setData()
which modifies data
, but the useEffect has data
as a dependency so this results in an infinite loop sending out a lot of requests to the backend as well as lagging out the frontend.
To replicate:
- Create a cut
- Add an answer to the cut
- Hide the cut (resulting in the answer being deleted)
- Wait a bit and the requests should start pouring in