Skip to content

L20 Reactive dependencies

22 minSuggested first: L19 Actions and reducers

Outcome

Predict which changes cause an effect to rerun under a stated tracking model.

Learn

In this toy model, an effect tracks the reactive properties it read during its most recent run. A write to a tracked property schedules a rerun. On a rerun, the set of dependencies is replaced by the reads from that run.

Real frameworks differ in batching, triggering, and how they clean up dependencies; read the code before inferring timing.

Worked example

show starts true. Then show becomes false, and after that count changes. Which of those writes reruns the effect?

JavaScript
// effect and state are supplied by the toy reactive system.
effect(() => {
  if (state.show) render(state.count);
  else render("hidden");
});
Predict first, then show the worked explanation

With show = true, the effect reads show and count. Changing show to false reruns it, and this time it reads only show. So the later count change doesn't rerun it. (Each run is assumed to finish before the next event.)

Practice and check

Guided practice

Practice, not scored

This time show starts false. Which dependency set does the first run record?

Choose one answer

Exit check

Counts toward completion

Still starting from show false, set show to true and let the rerun finish. Which writes can rerun the effect now?

Choose one answer

Common mistake

Treating every property mentioned in the source as an active dependency. Check what the effect actually read on the branch it took.

Practice on a real repository

Use the puzzle below to investigate dependency cleanup in real source.

Related topics: Reactivity

Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.

References