Comment by skrebbel

Comment by skrebbel 10 hours ago

1 reply

Yeah stuff like useEffect which is supposedly a function that "lets you synchronize a component with an external system" [0]

So eg when you want to focus an input, how do you do that? That's the input itself right, that's my core UI, that's not synchronizing, it's not an external system so I'm not supposed to use useEffect for that, right? That'd be bad, no?

Turns out I do need useEffect, and in fact it's the only way, barring using 3rd party hooks or components that, themselves, use useEffect for this. And the idea is (I assume?) that the DOM is the external system! This absolutely bonkers! The DOM is my app! That's not an external system at all. It's as non-external as things can get and I'm not synchronizing anything, I'm focusing an input.

This entire "external system" story isn't at all about what useEffect is, it's not what it does, it's merely what the React designers have decided you should use it for.

useEffect lets you run side effects. That's it, that's all there is to it. But they rewrote the docs with total beginners in mind, and put them so full of dos and donts that they forgot to explain what stuff actually does. Gaaah.

And half the documentation is like this. It dances around the actual behavior, never really explicitly saying what things do or how they work, with huge rants about what I ought to do and no info, except maaayybe hidden in some expando, about how things actually work and why.

[0] https://react.dev/reference/react/useEffect

rimunroe 10 hours ago

What's the condition in which you're trying to focus that input? Usually you're doing that in response to some sort of user action, in which case the time to handle that is within an event handler.

> And the idea is (I assume?) that the DOM is the external system! This absolutely bonkers! The DOM is my app!

External systems usually means stuff like an event system, network requests, or something else not managed directly by React. Unless you're reaching outside the area of the DOM React is managing, you can usually do this in event handlers or (for spookier cases) ref callbacks. There are certainly exceptions, but it's often an architectural smell.

Further down in the docs you'll see[0]:

> Effects are an “escape hatch”: you use them when you need to “step outside React” and when there is no better built-in solution for your use case.

[0] https://react.dev/reference/react/useEffect#wrapping-effects...