1. Concept library
  2. Web Dev with React
  3. Module 5: Effects and data fetching

Concept

StrictMode double effects

In development, StrictMode mounts each component, runs its effects, cleans them up and runs them again. It is React checking that your cleanup really undoes your effect; in production it runs once.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain why StrictMode runs effects twice in development
  2. Predict what a correct effect and a broken one do under StrictMode
  3. Spot the missing cleanup behind a duplicated request

In practice

A few lines that show StrictMode double effects doing its job.

TSX
useEffect(() => {
  const socket = connect(roomId);        // runs, is cleaned up, runs again
  socket.on("message", addMessage);
  return () => socket.close();           // without this, two live sockets
}, [roomId]);

// Correct cleanup makes the double run harmless.

The common mistake

Watch for

Removing StrictMode to make a duplicated request or subscription "go away". The duplication is the symptom of a missing cleanup, which will also bite when roomId changes in production.

What to know first

Your roadmap puts these before StrictMode double effects, unless you already know them.

How it stays learned

Once a session shows you understand StrictMode double effects, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

The review card

What you are asked on review day.

Web Dev with React · Effects and data fetching

Why does useEffect run twice in development with StrictMode?

Show answer

React mounts, unmounts and remounts components to surface effects that are missing cleanup. It only happens in development.

Previous in the module← useEffect and cleanup