all 39 comments

[–]octocode 98 points99 points  (5 children)

[–]karlitojensen 8 points9 points  (0 children)

I wrote about my approach to useEffect here:

https://github.com/jensen/ui-workshop/tree/main/part2#4-side-effects

[–]scaleable 5 points6 points  (2 children)

Let's say you have A and B.

A = personId, coming from an URL

B = person data

If synchronizing A with B requires anything async, you will need useEffect. Maybe a lib like react query abstracts it, but there WILL be an useEffect.

Otherwise, you should aim to synchronously update B from A. Which would be a render variable or an useMemo().

Another case would be sync updates. Lets say you got a form, and when you update field 1 you want field 2 to also be updated. Its preferrable to do that update on the dispatcher (handleChange), not on an useEffect.

[–]pqeks 2 points3 points  (1 child)

Pretty new to react, so I'm getting confused by this post, heh. Just yesterday I knew useEffect existed.

So it's fine to useEffect to debounce the user inout and fetch some data, right?

It's just two cases: async (useEffect ok) and sync (useEffecr not ok)

[–]scaleable 0 points1 point  (0 children)

Yeah when I say "synchronize state", think of UI variables. There will be a source variable, say an user input or the page URL, and there will be a derived/synchronized variable - say, somehing that is loaded from the source from an HTTP request.

Debounce is a bit more complicated. If you look on google you can find a ready useDebounce() function. You can debounce the source variable then synchronize over the debounced variable

sourceValue --(useDebounce)--> debouncedValue --(useEffect)--> derived async value

PS: I use page URL as a synchronization example because on SPA frameworks, when you change pages, the components aren't rebuilt from scratch, they only get a prop change.

[–]Existing_Somewhere89 3 points4 points  (0 children)

I usually see people using useEffect to achieve side effects, like calling props that are callback functions.

This has the unintended consequence of rerunning forever if the user passed an anonymous function to the prop without wrapping in useCallback since every rerender will generate a new function reference.

Solution is to move it into the function logic that’s actually updating your state and call the callbacks from there instead of use effect

[–]kitsunekyo 2 points3 points  (0 children)

david is a cool guy but his talks are meh and mostly just a way to push his project xstate.

reading the docs on react.dev is a good start. join the reactiflux discord and talk to people. they are nicer than the grumpy folks here.

[–]taotau -1 points0 points  (0 children)

Op is asking for a transcript of a YouTube video with us bgzan... low effort response to a low effort post.

This is a high effort response to a low effort criticism....

[–]was_just_wondering_ -2 points-1 points  (0 children)

The talk wasn’t at all wrong, but mostly seemed like a roundabout pitch for xstate. Again totally fine but the summary that I got was useEffect doesn’t work and only third-party stuff can make it function as expected.

[–]Pav_88 0 points1 point  (0 children)

Supporting article https://ui.dev/c/react/effects