← Back To ListJuly 21, 2023

Understanding useEffect hook

Understanding the component lifecycle and how the UI is rendered on browser is essential on the journey of React, it can reduce the bugs caused by misunderstanding the concept.

Here is a great article that break down the component rendering process.

https://overreacted.io/a-complete-guide-to-useeffect

diagram of React Hook Flow

React Hook Flow Diagram

mental model of rendering a simple counter component

function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    document.title = `You clicked ${count} times`;
  });

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

fig.1 diaglog of react with component and browser during a render

fig.2 diaglog of react with component and browser during 2nd render

Key takeout

  • each render has its own states and props, sees its own state values
  • each render has its own Event Handlers
  • each render has its own Effects

Related:

This is to test newletter function