Token导航 LogoToken导航TokenDH.com
开发只读github未标认证来源可访问许可证需确认审计通过

react-use-stateReact USE state 开发

Agent Skill

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

总安装

474

周安装

19

GitHub Stars

235

下载量

154
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:react-use-state(React USE state 开发)
来源仓库:https://github.com/flpbalada/my-opencode-config
仓库路径:skills/react-use-state
安装命令:
npx skills add https://github.com/flpbalada/my-opencode-config --skill react-use-state
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/flpbalada/my-opencode-config --skill react-use-state

简介

协助 React 项目中 useState 状态管理的最佳实践应用。

  • 适用于组件内部状态初始化、更新策略和类型安全配置。
  • 从指定 GitHub 仓库安装技能,供开发工具直接引用。
  • 输出代码应匹配项目所用 React 版本和 TypeScript 配置。
  • react-use-state 属于开发类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

React: useState Hook Best Practices

Core Concept

useState is a React Hook that adds a state variable to your component, triggering re-renders when the state changes.

const [state, setState] = useState(initialState);

When to Use useState

Ideal Use Cases

Use CaseExample
Form inputsconst [name, setName] = useState('')
UI stateconst [isOpen, setIsOpen] = useState(false)
Simple countersconst [count, setCount] = useState(0)
Local component dataconst [items, setItems] = useState([])

Use useState When

  • State is local to the component
  • State transitions are simple (direct value replacement)
  • Changes should trigger re-renders
  • You need to persist values between renders

When NOT to Use useState

Use useRef Instead

When you need mutable values that don't trigger re-renders:

// Interval IDs, DOM references, previous values
const intervalRef = useRef(null);
const inputRef = useRef(null);

Use useReducer Instead

When state logic is complex:

// Multiple related values, complex transitions
const [state, dispatch] = useReducer(reducer, initialState);

Use useReducer when:

  • State has multiple sub-values
  • Next state depends on previous state in complex ways
  • You want to centralize state logic

Avoid Redundant State

If a value can be computed from props or other state, don't store it:

// BAD: Redundant state
const [fullName, setFullName] = useState('');
useEffect(() => {
  setFullName(`${firstName} ${lastName}`);
}, [firstName, lastName]);

// GOOD: Compute during render
const fullName = `${firstName} ${lastName}`;

// If expensive, use useMemo
const sortedItems = useMemo(() =>
  items.sort((a, b) => a.name.localeCompare(b.name)),
  [items]
);

Don't Use for Global/Shared State

For state shared across multiple components:

  • React Context for moderate sharing
  • External stores (Zustand, Jotai) for complex apps
  • Server state libraries (TanStack Query) for async data

Critical Rules

1. Never Mutate State Directly

// BAD: Mutation
obj.x = 10;
setObj(obj); // React ignores this!

// GOOD: Create new object
setObj({ ...obj, x: 10 });

// BAD: Array mutation
arr.push(item);
setArr(arr); // React ignores this!

// GOOD: Create new array
setArr([...arr, item]);

2. State Updates Are Asynchronous

function handleClick() {
  setCount(count + 1);
  console.log(count); // Still old value!

  // If you need the new value:
  const nextCount = count + 1;
  setCount(nextCount);
  console.log(nextCount); // New value
}

3. Use Updater Function for Sequential Updates

// BAD: Only increments by 1
function handleClick() {
  setCount(count + 1); // 0 + 1 = 1
  setCount(count + 1); // 0 + 1 = 1 (same stale value!)
  setCount(count + 1); // 0 + 1 = 1
}

// GOOD: Increments by 3
function handleClick() {
  setCount(c => c + 1); // 0 -> 1
  setCount(c => c + 1); // 1 -> 2
  setCount(c => c + 1); // 2 -> 3
}

4. Use Initializer Function for Expensive Initial Values

// BAD: createTodos() runs every render
const [todos, setTodos] = useState(createTodos());

// GOOD: createTodos runs only once
const [todos, setTodos] = useState(createTodos);

// Or with arrow function for arguments
const [todos, setTodos] = useState(() => createTodos(userId));

5. Call Hooks at Top Level Only

// BAD: Conditional hook
if (condition) {
  const [state, setState] = useState(0); // Error!
}

// GOOD: Always call, conditionally use
const [state, setState] = useState(0);
if (condition) {
  // use state here
}

Common Patterns

Resetting State with Key

// Parent controls reset via key
<Form key={version} />

// When version changes, Form remounts with fresh state

Storing Functions in State

// BAD: Function gets called
const [fn, setFn] = useState(someFunction);

// GOOD: Wrap in arrow function
const [fn, setFn] = useState(() => someFunction);
setFn(() => newFunction);

Updating Objects/Arrays

// Object: spread and override
setForm({ ...form, email: newEmail });

// Nested object
setUser({
  ...user,
  address: { ...user.address, city: newCity }
});

// Array: filter, map, spread
setItems(items.filter(i => i.id !== id));        // Remove
setItems([...items, newItem]);                    // Add
setItems(items.map(i => i.id === id ? {...i, done: true} : i)); // Update

Quick Reference

DO

  • Use for simple, local component state
  • Create new objects/arrays when updating
  • Use updater function when depending on previous state
  • Use initializer function for expensive initial values

DON'T

  • Store computed/derived values
  • Mutate existing state objects/arrays
  • Read state immediately after setting (it's a snapshot)
  • Call setState unconditionally during render

Alternative Hooks Comparison

HookUse When
useStateSimple state, primitives, basic objects
useReducerComplex state logic, multiple sub-values
useRefMutable values without re-renders
useMemoExpensive computed values
useContextState shared across component tree

References

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

37.81%
按下载量换算58

Claude

30.82%
按下载量换算47

Cursor

19.64%
按下载量换算30

Gemini CLI

8.95%
按下载量换算14

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills