Skip to main content
InterviewHubPRO
Section Overview
Q8

Question 8 of 50

Part 1: Nuxt 3 Fundamentals (Junior / Entry-Level)

16%
Nuxt & Universal SSR (50 Qs)•Part 1: Nuxt 3 Fundamentals (Junior / Entry-Level)

What is `useState()` in Nuxt 3 and how does it prevent SSR memory leaks?

senior LevelCode ExampleHooksSSR & HydrationMemory Management

In Simple Words: useState() is Nuxt's SSR-friendly reactive state replacement for Vue's ref().

Why not use a global const state = ref() in Nuxt?

In an SSR Node.js server, module-level variables are shared across all incoming HTTP requests from all users. Storing user-specific data in a global ref() causes critical data leaks between different users! useState() creates a unique, request-scoped reactive state that is serialized into the initial HTML payload (under window.__NUXT__.data) and cleanly hydrated on the client without re-executing.

typescript
// composables/useUser.ts
export const useCurrentUser = () => {
  // Key 'auth-user' ensures unique hydration state across client/server
  return useState<User | null>('auth-user', () => null);
};