Skip to content
Discussion options

You must be logged in to vote

For app router (Next.js 13+), use these patterns by complexity:

  1. Simple (URL state)
// Persists in URL, works across pages
const router = useRouter()
const [tab, setTab] = useSearchParamsState('tab', 'all')

// ?tab=all persists navigation
  1. Client-side only (Zustand - 2KB)
// store/user.ts
import { create } from 'zustand'
export const useUserStore = create((set) => ({
  user: null,
  setUser: (user) => set({ user })
}))

// Any component: useUserStore((state) => state.user)
  1. Server-side (Next.js default)
// Persists via database/cookies
'use server'
export async function updateUser(formData) {
  'use server'
  // Update DB, redirect preserves state
}

Recommendation: URL state for fi…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by PanthersHowl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
npm
Labels
Question Ask and answer questions about GitHub features and usage npm Discussions around programming langages, open source and software development
2 participants