Menu
CoddyTech

Lifting Shared State

Part of the Fundamentals section of Coddy's Vue journey. Lesson 38 of 55.

When siblings must show the same value, keep that state in their parent. Each child receives the value as a prop and requests changes with an event.

const total = ref(7);
// <ScoreView :score="total" @add="total++" />
// <ScoreView :score="total" @add="total++" />

Both child displays then derive from one source of truth. Separate child refs would drift apart, and copying a prop into a ref only captures its initial value. Keep local state in a child when it is truly private, but lift state when multiple components must coordinate.

challenge icon

Challenge

Medium

Two panels must show one shared donation total, initially 4. Put the ref in App.vue. Render TotalPanel.vue inside #left and #right, passing an amount prop. Each child displays it in p.amount and emits increase from a button. Either button must update both views through the parent.

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals