Communitygithub.com

ComeOnOliver/skillshub

管理狀態(伺服器URL vs 客戶端Hook)的最佳實踐。在用於管理 Next.js 應用程式中的 URL 狀態、客戶端狀態或全域狀態時使用。(觸發條件:**/hooks/*.ts, **/store.ts, **/components/*.tsx, useState, useContext, zustand, redux)

skillshub 是什麼?

此技能提供了一套完整的 Next.js 狀態管理指南與最佳實踐,專為開發者透過 URL、伺服器端資料及客戶端 UI 狀態管理而設計。其核心原則是將 URL 作為可分享、持久化狀態的單一事實來源,並建議使用 `useSearchParams()` 和 `useRouter()` 來處理;針對伺服器端資料則推薦 SWR 或 TanStack Query 以避免手動 `useEffect` 的資料擷取。對於客戶端 UI 狀態,技能推薦 Zustand 或 Jotai,並在低頻資料情境下合理使用 Context API,同時強調應盡可能將狀態與所使用的元件就近放置。支援的平台包括 Next.js 15+(含 `useOptimistic`)、Zustand、Redux 等。此技能透過實際程式碼範例及反模式提醒,協助團隊維持可維護、高效能的應用架構。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/HoangNguyen0403/agent-skills-standard/nextjs-state-management

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

狀態管理

優先級:P2(中)

實作指南

  • URL 狀態:將 URL 作為可分享/持久化狀態的單一事實來源。URL 參數:可在重新載入及連結間共享。透過 useSearchParams() 存取,並以 useRouter() 更新。模式:const params = new URLSearchParams(searchParams); params.set('q', term); useRouter().replace(...) 。
  • 伺服器狀態:使用 SWR 或 TanStack Query (React Query) 來快取及擷取伺服器資料。避免手動將伺服器資料同步至 useState。
  • 客戶端狀態:使用 Zustand(create<CartState>()((set) => ({ ... })))——僅在 'use client' 元件中使用——或 Jotai 處理複雜、高頻率的 UI 狀態。僅對低頻率資料使用 Context API 來避免 Props Drilling。
  • 狀態提升:盡可能將狀態與其使用的元件就近放置。僅當狀態需在兄弟元件間共享時,才將其提升至父層。
  • Next.js 15+ 整合:確保客戶端元件中的狀態更新不與伺服器端渲染邏輯衝突。使用 useOptimistic Hook 管理樂觀更新。
  • 狀態水合:在客戶端元件中從 localStorage 初始化狀態時需小心,以避免水合錯誤。請包裝在 useEffect 中或使用 mounted 標記。

2. URL 驅動狀態(搜尋/篩選)

使用 useSearchParams + useRouter 來更新 URL 參數。請參閱 URL 狀態模式。

3. 伺服器狀態(TanStack Query / SWR)

若需在客戶端取得「即時」資料(例如輪詢股票價格、聊天功能),請勿手動使用 useEffect 以 fetch 處理。請使用程式庫。

// 自動快取、去重及重新驗證,並搭配 refreshInterval
const { data, error } = useSWR('/api/user', fetcher, {
  refreshInterval: 30000,
});

程式庫模式

針對特定狀態管理模式,請參閱:

反模式

  • 不要為簡單狀態使用全域儲存:使用 useState 或 URL 參數;避免為基本的 UI 使用 Zustand。
  • 不要在狀態中存放大型物件:將其分解為細粒度的原始類型,以防止額外重新渲染。
  • 不要使用 useEffect 進行資料擷取:使用 SWR 或 TanStack Query 處理伺服器狀態。
  • 不要在客戶端儲存中存放伺服器狀態:在 RSC 中擷取;客戶端儲存僅用於 UI 狀態。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

A conceptual skill for building an API client in Next.js that handles JWT tokens

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相關技能