3 ms·
There are a couple of potential bugs in the useEffect one. Firstly, the two examples aren't semantically equivalent. onSuccess can change between renders, so i
by andrewingram 6y ago
There are a couple of potential bugs in the useEffect one.
Firstly, the two examples aren't semantically equivalent. onSuccess can change between renders, so in the "wrong" version, the version of onSuccess that'll be executed is the latest one. Whilst in the "correct" version, it'll be the one defined during the render which was in play during the initial render.
If you always want to make use of the latest onSuccess in the "correct" version, you'll probably want to look into putting it on a ref.
Another issue with the "wrong" version is that onSuccess will be called a second time if its identity changes after it's already been called once, which is quite likely if its parent re-renders and doesn't make use of useCallback.
Ultimately I think the "correct" version is indeed better, but the question of which is the correct onSuccess handler is one that always needs to be seriously considered.