Skip to content
Tutorials Center
My learning

Topics

Lesson 78 of 87 ยท Projects

Project: Quiz App

Build a quiz app in Vue 3 in two stages: the question flow with scoring and feedback, then a shared store, animated transitions between questions, keyboard answers and a results review.

A quiz is a friendly project because the rules are simple and the screen changes a lot: a question, four buttons, feedback, the next question, and a final score. Under the surface it teaches the most important Vue habit, which is deciding what the state is and letting everything else be calculated.

You will build it in two stages. Stage 1 is the complete flow inside one component, using refs and computed properties. Stage 2 moves the state into a store (the Pinia pattern from Pinia basics), adds transitions between questions and keyboard answers, and ends with a review of the questions you missed.

Design first

What does the app remember?

  • The list of questions (fixed data).
  • Which question we are on (index).
  • What the player chose for each question (answers).

Everything else is derived. The current question, the score, whether the quiz is finished and the progress percentage can all be computed from those three things. If you stored the score separately, you would have to keep two numbers in agreement; if it is computed, it cannot disagree.

flowchart LR
    Q["questions (data)"] --> D["current, score, finished, progress<br/>(computed)"]
    I["index"] --> D
    A["answers"] --> D
    D --> V["View"]
    V -- "pick(i), next(), restart()" --> A

State is the arrows into the box; everything in the box is calculated.

Stage 1: the question flow

One component holds the state. After the player picks an answer, the buttons lock, the right and wrong choices are coloured, and a Next button appears. Picking again is ignored, so the score cannot be cheated by clicking twice.

Stage 1: questions, feedback and a score

Read the decisions:

  1. picked doubles as a lock. null means "not answered yet". The same value drives the button colours (stateOf), the disabled state, and whether the explanation and Next button are visible. One piece of state, several uses.
  2. finished is computed. There is no separate "screen" variable to get out of step with index.
  3. The score is a ref here, incremented when the answer is right. That is simple, but it is state we could derive. Stage 2 fixes it.
  4. Guard against impossible actions. pick returns early when already answered; the buttons are also disabled. Do both: the UI hint and the rule in the code.

The weakness: the player cannot go back to see what they got wrong, the score is stored rather than derived, the question simply swaps with no animation, and all the logic is tied to this one component. Time for a store.

Stage 2: a store, transitions and a review

We move the state into a store and let components read it. The store keeps only index and answers (an array with the chosen option per question). score, finished, progress and the list of missed questions are computed. The runner has no Pinia, so a tiny defineStore stand-in creates one shared reactive object (see what the stand-in leaves out); with real Pinia, the store body is the same.

Two more features. A <Transition mode="out-in"> keyed by the question index slides the old question out before the new one slides in. And a keydown listener lets players answer with the keys 1 to 4 and continue with Enter, which is added in onMounted and removed in onUnmounted (lifecycle hooks).

Stage 2: a store, transitions, keyboard and review

Press 2, then Enter, and carry on with the keyboard. Get one wrong on purpose and look at the review list at the end.

What changed, and why it is better:

  • The store owns the rules. pick refuses a second answer, next refuses to advance before an answer, and restart resets everything. Components cannot break those rules because they only call actions.
  • score is derived from answers. There is nothing to keep in sync, and the review list (missed) comes from the same data for free. Storing the player's choices instead of a number is what makes a review possible.
  • The transition is keyed. Changing :key makes Vue treat the card as a new element, so the old one leaves and the new one enters. mode="out-in" stops both from being on screen at once.
  • Window listeners are cleaned up. Adding in onMounted and removing in onUnmounted prevents duplicate handlers if the app is mounted again.

Where to go next

  • Load the questions from a JSON file or an API with the fetching pattern, including loading and error states.
  • Add a countdown per question with setInterval (and clear it with onUnmounted).
  • Shuffle the questions and the options at the start. Keep the original order in the data and shuffle a copy, so answer indexes stay correct.
  • Save the best score to localStorage in a real project (composable patterns shows a storage ref).
  • Make the buttons accessible: group them with role="radiogroup", announce feedback with aria-live="polite" (accessibility).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does Stage 2 store the player's chosen answers instead of a score number?

    Store the minimum that is true, and compute the rest. Answers give you the score, the missed list and the progress without extra state.
  2. 2. What does :key="quiz.index" on the question card do inside <Transition>?

    Without a changing key, Vue would reuse the same element and just patch its text, so there would be nothing to animate.
  3. 3. Why is the keydown listener removed in onUnmounted?

    Listeners added to window outlive the component unless you remove them.
  4. 4. Where should the rule 'ignore a second answer to the same question' live?

    Rules belong in the action so they hold no matter how the UI changes. Disabled buttons are a helpful hint on top.

Summary

  • Decide what the state is (questions, index, answers) and compute everything else (current question, score, progress, finished, missed).
  • Use one value (picked) for several jobs: lock, colours and the visibility of feedback.
  • Put rules in store actions, and let components call them.
  • A keyed <Transition mode="out-in"> animates between questions.
  • Add window listeners in onMounted and remove them in onUnmounted.

Next: charts, forms and saved data, Project: Expense Tracker.