The value of useEffect is in the guarantees it makes around being a safe place to do side effects. Render can be called in cases where the component didn't render, and generally React is built around an assumption that you won't do side effects there.
Consider that you don't even have the option to optimize this way with lifecycles either. If you want to do something conditionally based on state after some renders with a class component, you do it in componentDidUpdate by testing those state values. But componentDidUpdate always runs.
Part of the mindset shift is to not think about these "when" conditions for your semantic code, and to instead model the state explicitly. If something shouldn't run for every render, then it should run conditionally based on some state. The deps array shouldn't be needed for that.
You're right, in some cases you probably couldn't get away with putting side effects outside of the useEffect. However, using the second argument is still a powerful tool that should be used and doesn't mean you are "incorrectly coding functionality"
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey Abdisalan,
The value of useEffect is in the guarantees it makes around being a safe place to do side effects. Render can be called in cases where the component didn't render, and generally React is built around an assumption that you won't do side effects there.
Consider that you don't even have the option to optimize this way with lifecycles either. If you want to do something conditionally based on state after some renders with a class component, you do it in componentDidUpdate by testing those state values. But componentDidUpdate always runs.
Part of the mindset shift is to not think about these "when" conditions for your semantic code, and to instead model the state explicitly. If something shouldn't run for every render, then it should run conditionally based on some state. The deps array shouldn't be needed for that.
You're right, in some cases you probably couldn't get away with putting side effects outside of the useEffect. However, using the second argument is still a powerful tool that should be used and doesn't mean you are "incorrectly coding functionality"