Skip to content
Learn Netverks

Lesson

Step 18/36 50% through track

setup-function

The setup function

Last reviewed May 28, 2026 Content v20260528
Track mode
client_vue
Means
In-browser Vue TS
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches The setup function: the concepts, APIs, and habits you need before advancing in Vue.

Without The setup function, you will struggle to read or extend Vue codebases and playground exercises.

You will apply The setup function in contexts like: Greenfield SPAs, dashboards, design systems, and full-stack apps that pair Vue with PHP or Node APIs.

Write TypeScript, click Run—Vue 3 loads from CDN with the template compiler, mountApp shows UI in #app, and printOutput feeds the terminal.

When you can explain the previous lesson's ideas without copying starter code.

setup() is the entry point for Composition API logic. It runs before the component renders, receives props and a context object (emit, slots, attrs), and returns data and functions for the template.

Execution timing

  • Runs once when the component instance is created (before the template renders)
  • props are reactive—use toRefs if you need to destructure without losing tracking
  • Return plain objects; refs unwrap in templates automatically

Compared to React function components

React mixes render and hooks in one function body that runs every render. Vue’s setup() runs once—reactive refs and computeds persist across renders. Template re-evaluation is separate from re-running all setup logic.

Organizing setup

Group related refs, computeds, and methods together—or extract composables (later lesson). Avoid massive single setup() blocks in production; colocate by feature.

Important interview questions and answers

  1. Q: Does setup run on every render?
    A: No—only once per instance; reactivity drives subsequent updates.
  2. Q: Can setup be async?
    A: Not directly as async function—use onMounted + async work or Suspense patterns in full apps.

Self-check

  1. What does setup() return?
  2. How do you access emit inside setup?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • What setup returns?
  • this in setup?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump