Why would we use useEffect without a dependency array?

For this exact case you’re right because undefined is passed as the dependencies of useEffect. This means useEffect runs on every render and thus the event handlers will unnecessarily get detached and reattached on each render. function listener() { console.log(‘click’); } function Example() { const [count, setCount] = window.React.useState(0); window.React.useEffect(() => { console.log(`adding listener ${count}`); … Read more

Updating state to the same value directly in the component body during render causes infinite loop

TL;DR The first example is an unintentional side-effect and will trigger rerenders unconditionally while the second is an intentional side-effect and allows the React component lifecycle to function as expected. Answer I think you are conflating the “Render phase” of the component lifecycle when React invokes the component’s render method to compute the diff for … Read more