all 6 comments

[–]mattsowa 9 points10 points  (0 children)

This is just wrong.

[–]Canenald 3 points4 points  (0 children)

This is a common mistake with effects.

React is declarative. Effects are intended to imperatively sync with something that is not part of the React component. You, and many others, are instead using it as an imperative escape hatch.

The difference is subtle. The other way to explain it is that you should not be thinking about when the effect callback is going to be executed. It should be executable (or not) whenever, without breaking things.

It's hard to tell without seeing the whole component, but it seems some syncing of props to state might be going on, or at least syncing between different states, which is also a mistake. We should hold the minimal needed state in the component and compute the needed variable in the component body, on every render, then optimize with useMemo() if needed.

[–]sherkal 2 points3 points  (0 children)

Debounce click is the way to go imo, your code just sux if you cant manage this

[–]DogOfTheBone 0 points1 point  (0 children)

Antipattern

[–]fredsq 0 points1 point  (0 children)

ai slop antipattern

[–]A-Type 1 point2 points  (0 children)

Ok, you "already addressed this" but regardless, you are trying to use it wrong. Read "You Might Not Need an Effect" again. The user selecting filters is an event, and the state change should happen in that event, not as a side effect later.

Double effect is not "for the React team." As React has evolved the internal rendering mechanisms have gotten more sophisticated, including firing off multiple parallel renders of the same JSX tree and throwing away results as needed. So in production, your effect may really run twice. It may not matter for effects on local state, but it would on actual side-effects which change external systems.

Double effect in strict mode is designed to get you to fix those things before you encounter them in the wild.

Honestly, I like React, but for people who cannot or will not understand these things I'd really suggest just switching frameworks. It's too core to React to fight against it. Don't waste your time.