Thuta Learning
Vue.js
ExercisesWeb DevelopmentBeginner

Practice: Components, Router & State Challenge

Vue.jsLesson 2240 min

What you'll walk away with

  • Practice the Components, Router & State Challenge hands-on
  • Reinforce the skills you've already learned through practice
  • Get comfortable finding bugs, fixing them, and checking your own work

Explain it simply

Practice: Components, Router & State Challenge, in plain language

This lesson is about combining everything you learned in the Intermediate/Advanced chapter — component communication, composables, routing, and state management — all at once. The tasks are designed to mirror scenarios you'd actually run into in a real production app. We're not teaching new concepts here; we're testing whether you can combine what you already know. If you finish Task 1 before moving on to Tasks 2 and 3, you'll see how the logic connects more clearly.

Problem → Why → Solution

The problem — why this concept matters

Mutating a prop (item) directly from the child component, causing it to fall out of sync with the parent's state

A common mistake

Destructuring a Pinia store's reactive object directly in a component (const { items } = store), which breaks reactivity

Core concept — understand how it works

3 more challenging practice tasks that combine props/emit, composables, Vue Router, and the Pinia store.

Mental model
  1. requirement
  2. reason and implement
  3. test and explain

Practice the Components, Router & State Challenge hands-on

Core syntax or pattern

javascript
// useCounter.js (Task 2)
import { ref } from 'vue'

export function useCounter(start = 0) {
  const count = ref(start)
  const increment = () => count.value++
  const decrement = () => count.value--
  return { count, increment, decrement }
}

// stores/cart.js (Task 3)
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    total: (state) => state.items.reduce((sum, i) => sum + i.price, 0)
  },
  actions: {
    addItem(item) {
      this.items.push(item)
    }
  }
})

// ListItem.vue child (Task 1)
// props: { item: Object }
// emits: ['delete']
// template button: @click="$emit('delete', item.id)"

3 more challenging practice tasks that combine props/emit, composables, Vue Router, and the Pinia store.

Read the code step by step

  1. requirement// useCounter.js (Task 2)

    This lesson is about combining everything you learned in the Intermediate/Advanced chapter — component communication, composables, routing, and state management — all at once. The tasks are designed to mirror scenarios you'd actually run into in a real production app. We're not teaching new concepts here; we're testing whether you can combine what you already know. If you finish Task 1 before moving on to Tasks 2 and 3, you'll see how the logic connects more clearly.

  2. reason and implementgetters: {

    3 more challenging practice tasks that combine props/emit, composables, Vue Router, and the Pinia store.

  3. test and explain// template button: @click="$emit('delete', item.id)"

    Apply Practice: Components, Router & State Challenge in a small example and test normal, edge, and failure cases.

Execution flow
  1. requirement
  2. reason and implement
  3. test and explain

Success

Practice the Components, Router & State Challenge hands-on

Failure

Mutating a prop (item) directly from the child component, causing it to fall out of sync with the parent's state

Hold on, let's think about it this way

This lesson is about combining everything you learned in the Intermediate/Advanced chapter — component communication, composables, routing, and state management — all at once. The tasks are designed to mirror scenarios you'd actually run into in a real production app. We're not teaching new concepts here; we're testing whether you can combine what you already know. If you finish Task 1 before moving on to Tasks 2 and 3, you'll see how the logic connects more clearly.

Exercises

Task 1: Pass an 'items' array from the parent component down to a child list component as props, and when the delete button in the child is clicked, emit('delete', id) to send the event back to the parent to delete the item. Task 2: Write a composable called useCounter() that returns count, increment, and decrement, then import it into two components at the same time and prove that each component's state stays independent. Task 3: Build a Pinia store called 'useCartStore' with an addItem action and a total price getter, then use Vue Router to navigate between two routes ('/cart' and '/checkout') and verify that the store data doesn't disappear.

Code Example

javascript
// useCounter.js (Task 2)
import { ref } from 'vue'

export function useCounter(start = 0) {
  const count = ref(start)
  const increment = () => count.value++
  const decrement = () => count.value--
  return { count, increment, decrement }
}

// stores/cart.js (Task 3)
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    total: (state) => state.items.reduce((sum, i) => sum + i.price, 0)
  },
  actions: {
    addItem(item) {
      this.items.push(item)
    }
  }
})

// ListItem.vue child (Task 1)
// props: { item: Object }
// emits: ['delete']
// template button: @click="$emit('delete', item.id)"
You should see
Clicking the delete button in the child removes the item from the parent's items array; the counters in each component using the composable count independently without affecting each other; and even after navigating from the cart page to the checkout page, the cart total price stays intact.

Try It in 5 Minutes

Create two components for Task 2 (CounterA.vue, CounterB.vue), import useCounter() into both, and use console.log to prove within 5 minutes that clicking the button in one doesn't affect the other's state.

A Word of Caution

If you want to destructure state from a Pinia store, you must use storeToRefs() — otherwise the reactivity link breaks and the UI stops updating.

Real-world usage

How to apply it in a real project

Apply Practice: Components, Router & State Challenge in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Create two components for Task 2 (CounterA.vue, CounterB.vue), import useCounter() into both, and use console.log to prove within 5 minutes that clicking the button in one doesn't affect the other's state.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
javascript
// useCounter.js (Task 2)
import { ref } from 'vue'

export function useCounter(start = 0) {
  const count = ref(start)
  const increment = () => count.value++
  const decrement = () => count.value--
  return { count, increment, decrement }
}

// stores/cart.js (Task 3)
import { defineStore } from 'pinia'

export const useCartStore = defineStore('cart', {
  state: () => ({ items: [] }),
  getters: {
    total: (state) => state.items.reduce((sum, i) => sum + i.price, 0)
  },
  actions: {
    addItem(item) {
      this.items.push(item)
    }
  }
})

// ListItem.vue child (Task 1)
// props: { item: Object }
// emits: ['delete']
// template button: @click="$emit('delete', item.id)"

Expected: Practice the Components, Router & State Challenge hands-on

Show hint

Apply Practice: Components, Router & State Challenge in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Practice: Components, Router & State Challenge?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • requirement → reason and implement → test and explain
  • 3 more challenging practice tasks that combine props/emit, composables, Vue Router, and the Pinia store.
  • Mutating a prop (item) directly from the child component, causing it to fall out of sync with the parent's state
  • Apply Practice: Components, Router & State Challenge in a small example and test normal, edge, and failure cases.

Vue.js Guide — Vue.js

Easy traps

  • Mutating a prop (item) directly from the child component, causing it to fall out of sync with the parent's state
  • Destructuring a Pinia store's reactive object directly in a component (const { items } = store), which breaks reactivity

Now Try It Yourself

Create two components for Task 2 (CounterA.vue, CounterB.vue), import useCounter() into both, and use console.log to prove within 5 minutes that clicking the button in one doesn't affect the other's state.

You'll know it worked when: Clicking the delete button in the child removes the item from the parent's items array; the counters in each component using the composable count independently without affecting each other; and even after navigating from the cart page to the checkout page, the cart total price stays intact.

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Practice: Components, Router & State Challenge | Thuta Learning