MCP HubMCP Hub
返回技能列表

frontend-patterns

thkt
更新于 Today
40 次查看
3
3
在 GitHub 上查看
设计powerpointreactaidesign

关于

This skill provides framework-agnostic frontend component design patterns with rich React implementation examples. Use it when working with React, Vue, or Angular components to discuss patterns, hooks, state management, and architecture. It covers essential topics like Container/Presentational separation, React Hooks patterns, custom hooks design, and component composition strategies.

技能文档

Frontend Patterns - Component Design Guide

Overview

Framework-agnostic component design patterns with emphasis on React implementations. Covers:

  1. Container/Presentational - Logic/UI separation (universal pattern)
  2. Hooks Patterns - useEffect, useMemo, useCallback best practices (React-specific)
  3. Custom Hooks - Logic reuse and design patterns (React-specific)
  4. State Management - Context, props, and state organization (universal concepts, React examples)
  5. Component Composition - children, render props, HOC (universal patterns)

When to Use This Skill

Automatic Triggers

Keywords that activate this skill:

  • React, component, コンポーネント
  • pattern, パターン, design
  • hooks, custom hook, カスタムフック
  • container, presentational, 分離
  • state management, 状態管理
  • composition, HOC, render props

Explicit Invocation

For guaranteed activation:

  • "Apply frontend patterns skill"
  • "Use component design patterns"
  • "Show React patterns"

Common Scenarios

  • Designing component architecture
  • Implementing with /code command
  • Reviewing with design-pattern-reviewer agent
  • Refactoring component structure
  • Learning best practices for React/Vue/Angular

Core Patterns

1. Container/Presentational Pattern

Concept (Framework-agnostic): Separate logic (data fetching, state) from UI (presentation).

Benefits:

  • UI components are reusable
  • Easy to test separately
  • Clear separation of concerns

React Implementation:

// ❌ Avoid: Mixed concerns
export const UserProfile = () => {
  const [user, setUser] = useState(null)
  useEffect(() => {
    fetch('/api/user').then(setUser)
  }, [])
  return <div>{user?.name}</div>
}

// ✅ Good: Separated
// Container (logic)
export const UserProfileContainer = () => {
  const [user, setUser] = useState(null)
  useEffect(() => {
    fetch('/api/user').then(setUser)
  }, [])
  return <UserProfile user={user} />
}

// Presentational (UI only)
export const UserProfile = ({ user }) => (
  <div>{user?.name}</div>
)

Vue Implementation (for reference):

<!-- Container -->
<script setup>
import { ref, onMounted } from 'vue'
const user = ref(null)
onMounted(() => fetch('/api/user').then(u => user.value = u))
</script>

<!-- Presentational -->
<script setup>
defineProps(['user'])
</script>
<template><div>{{ user?.name }}</div></template>

2. Hooks Patterns (React-Specific)

useEffect Dependencies:

// ❌ Avoid: Missing dependencies
useEffect(() => {
  fetchData(userId)
}, []) // Missing userId

// ✅ Good: Complete dependencies
useEffect(() => {
  fetchData(userId)
}, [userId])

useMemo for Expensive Computations:

// ❌ Avoid: Recalculated every render
const filtered = items.filter(item => item.active)

// ✅ Good: Memoized
const filtered = useMemo(
  () => items.filter(item => item.active),
  [items]
)

useCallback for Stable Functions:

// ❌ Avoid: New function every render
<Child onClick={() => handleClick(id)} />

// ✅ Good: Stable reference
const handleClickCallback = useCallback(
  () => handleClick(id),
  [id]
)
<Child onClick={handleClickCallback} />

3. Custom Hooks Design (React-Specific)

Naming Convention: Always start with use

// ✅ Good: Reusable data fetching
function useFetchUser(userId) {
  const [user, setUser] = useState(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    fetch(`/api/users/${userId}`)
      .then(setUser)
      .finally(() => setLoading(false))
  }, [userId])

  return { user, loading }
}

// Usage
const { user, loading } = useFetchUser(userId)

4. State Management

Concept (Universal):

  • Local state: Component-specific data
  • Shared state: Data used across components
  • Global state: Application-wide data

React Implementation:

// Local state (useState)
const [count, setCount] = useState(0)

// Shared state (Context)
const ThemeContext = createContext()
// Provider
<ThemeContext.Provider value={theme}>

// Global state (Context + custom hook)
function useAuth() {
  const context = useContext(AuthContext)
  return context
}

State Granularity:

// ❌ Avoid: Large state object
const [state, setState] = useState({
  user, posts, comments, settings
})

// ✅ Good: Separate state
const [user, setUser] = useState()
const [posts, setPosts] = useState()
const [comments, setComments] = useState()

5. Component Composition

Concept (Universal): Build complex components from simple ones.

React Patterns:

// 1. Children pattern
const Card = ({ children }) => (
  <div className="card">{children}</div>
)

// 2. Render props
const DataProvider = ({ render, data }) => (
  <div>{render(data)}</div>
)

// 3. HOC (Higher-Order Component)
const withAuth = (Component) => (props) => {
  const user = useAuth()
  return user ? <Component {...props} /> : <Login />
}

Detailed Knowledge Base

Reference Documents

  • [@./references/container-presentational.md] - Complete Container/Presentational pattern guide with style responsibilities

Integration Points

With Agents

  • design-pattern-reviewer - Reviews React/frontend patterns using this skill's principles
  • root-cause-reviewer - Identifies architectural issues

With Commands

  • /code - Applies patterns during implementation
  • /review - Validates pattern usage

Integration Method

# In agent YAML frontmatter
dependencies: [frontend-patterns]

Or explicit reference:

[@~/.claude/skills/frontend-patterns/SKILL.md]

Quick Start

For New Component

  1. Start Presentational - UI only, props-driven
  2. Add Container - Wrap with logic when needed
  3. Extract Hooks - Reusable logic → custom hooks
  4. Optimize - useMemo/useCallback for performance

For Refactoring

  1. Identify mixed concerns - Logic + UI in same component
  2. Separate Container/Presentational
  3. Extract custom hooks - Shared logic
  4. Simplify state - Split large state objects

For Code Review

  1. Check separation - Container vs Presentational
  2. Verify hooks - Dependencies, memoization
  3. Evaluate composition - Reusability, flexibility
  4. Assess state - Granularity, management strategy

Best Practices

Do's ✅

  • Separate logic from UI (Container/Presentational)
  • Use custom hooks for reusable logic
  • Complete dependency arrays in useEffect
  • Memoize expensive computations
  • Stable callback references for child props
  • Props-only Presentational components

Don'ts ❌

  • Mix data fetching with UI
  • Inline functions in JSX (breaks memoization)
  • Large monolithic components
  • Missing useEffect dependencies
  • Premature optimization (measure first)
  • Global state for local concerns

Framework Comparison

PatternReactVueAngular
Container/PresentationalSeparate componentsComposition APISmart/Dumb components
StateuseState, Contextref, reactiveServices, RxJS
EffectsuseEffectonMounted, watchngOnInit, rxjs
Compositionchildren, render propsslots, scoped slotsng-content, directives

Key Insight: Patterns are universal, implementations differ.

When NOT to Use

Skip complex patterns for:

  • Simple, one-off components
  • Prototypes and experiments
  • Components without reuse
  • Over-abstraction (YAGNI)

Rule: Measure complexity. Add patterns when pain is felt, not anticipated.

Success Metrics

Patterns are working when:

  • Components are easily testable
  • UI components reuse across projects
  • Logic is shareable via custom hooks
  • Refactoring doesn't break unrelated code
  • New team members understand structure quickly

Resources

references/

  • container-presentational.md - Detailed Container/Presentational guide

scripts/

Currently empty (knowledge-only skill)

assets/

Currently empty (knowledge-only skill)

快速安装

/plugin add https://github.com/thkt/claude-config/tree/main/frontend-patterns

在 Claude Code 中复制并粘贴此命令以安装该技能

GitHub 仓库

thkt/claude-config
路径: skills/frontend-patterns

相关推荐技能

llamaguard

其他

LlamaGuard是Meta推出的7-8B参数内容审核模型,专门用于过滤LLM的输入和输出内容。它能检测六大安全风险类别(暴力/仇恨、性内容、武器、违禁品、自残、犯罪计划),准确率达94-95%。开发者可通过HuggingFace、vLLM或Sagemaker快速部署,并能与NeMo Guardrails集成实现自动化安全防护。

查看技能

sglang

SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。

查看技能

evaluating-llms-harness

测试

该Skill通过60+个学术基准测试(如MMLU、GSM8K等)评估大语言模型质量,适用于模型对比、学术研究及训练进度追踪。它支持HuggingFace、vLLM和API接口,被EleutherAI等行业领先机构广泛采用。开发者可通过简单命令行快速对模型进行多任务批量评估。

查看技能

langchain

LangChain是一个用于构建LLM应用程序的框架,支持智能体、链和RAG应用开发。它提供多模型提供商支持、500+工具集成、记忆管理和向量检索等核心功能。开发者可用它快速构建聊天机器人、问答系统和自主代理,适用于从原型验证到生产部署的全流程。

查看技能