Agent Skills › langfuse/langfuse › react-component-guidelines

react-component-guidelines

GitHub

提供React组件编写规范,涵盖最小接口、组合策略、显式状态及封装原则,旨在提升组件可复用性与类型安全。

.agents/skills/react-component-guidelines/SKILL.md langfuse/langfuse

Trigger Scenarios

创建新的React组件 重构现有React组件以符合规范

Install

npx skills add langfuse/langfuse --skill react-component-guidelines -g -y
More Options

Non-standard path

npx skills add https://github.com/langfuse/langfuse/tree/main/.agents/skills/react-component-guidelines -g -y

Use without installing

npx skills use langfuse/langfuse@react-component-guidelines

指定 Agent (Claude Code)

npx skills add langfuse/langfuse --skill react-component-guidelines -a claude-code -g -y

安装 repo 全部 skill

npx skills add langfuse/langfuse --all -g -y

预览 repo 内 skill

npx skills add langfuse/langfuse --list

SKILL.md

Frontmatter
{
    "name": "react-component-guidelines",
    "description": "Guidelines for writing React components. Use this when creating a new react component."
}

Components are useful because they act as an encapsulated unit and therefore promote composition. For this to work, a component needs to be isolatable and neither leak implementation details nor depend on its placement or usage context. The interface of a component is defined by its props, and therefore the props should be designed to be as explicit and unambiguous as possible.

Minimal Interface

  • No unused props
  • No default values unless they bring a MAJOR benefit for ergonomics, use your best judgement here.
  • Avoid optional props
  • No conflicting props (e.g. having both onClick & onSelect)

Composition

  • Shared components should own meaningful presentation or complex logic. There must be a reason for code to be a component over a pure function or hook. When most logic is JSX-independent, use a pure function and keep trivial JSX at each call site. Use a hook over a pure function only when React lifecycle or state is required.
  • Avoid polymorphic components:
    • Presentation: A component should own one concrete presentation. When an action needs different presentations across contexts (e.g. sometimes a button, sometimes an icon), keep them separate rather than selecting between them with mode props or flags.
    • Behavior: A component should own one cohesive workflow. When a prop selects fundamentally different workflows, split them into separate behavior-owning components. For example, prefer dedicated create, update, and delete dialog controllers over one action component whose mode changes the entire interaction.
  • Extract only what is shared across call sites. Reuse a presentational component when the presentation is shared; when only state or behavior is shared, encapsulate it in a wrapper/render-prop component and let each call site provide its own presentation.

Explicit States

  • Always prefer Pick<> over Omit<> for prop types as this is more explicit.
  • When spreading props, you must exclude the props that are manually applied on an element in the type definition of the component via Pick<>
  • Use discriminated unions to communicate intent instead of relying on nullability / optionality
  • Use discriminated unions to make impossible states impossible to represent in the type system instead of relying on runtime checks (e.g. if there is a loading and data type in the prop, then it should not be possible to have a state where loading is true and data is not null)

Encapsulation

  • No className / style props unless the component itself is a headless component that does not contain any styling or layout logic itself.
  • Internals such as cva classes or helper functions should not be exported

Ownership

  • Margin should be applied by the parent component, not contained in a component. The child component should only define the inner spacing of itself and its contents.
  • It's bad practice to have a component that returns null or undefined. Most of the time this suggests that the condition that leads to this state should be handled by the parent component instead, often this can be done in a way close to the current component by using a hook or a HOC.

Overlays

  • Compose overlays with DropdownMenuController, PopoverController, or DialogController. Trigger presentation should remain in the caller and not be abstracted. If additional behavior is needed, add a feature-specific wrapper that handles things such as permissions, analytics, mutations, or other workflow behavior that should be shared.
  • When overlay content depends on the action that opened it, let the controller own that transient state. Pass the complete payload to openDialog(payload) or openDrawer(payload) and render content from the controller state instead of hoisting selection and open state into the parent.
  • Keep the controller components outside transient overlays that can trigger it. A popover or dialog opened from a dropdown item must not be owned by DropdownMenuContent, which unmounts when the dropdown closes.
  • Consume the controller's render-prop controls instead of duplicating their shape. Do not move the passed Trigger into components. Abstract trigger presentation through a ref-forwarding component with explicit variants, while keeping <Trigger asChild> at each call site.
  • Compose <Trigger asChild> in the caller around a semantic child that forwards its ref and injected props. For example, use DropdownMenuItem directly rather than nesting a Button inside it.

Deterministic Styling

  • Avoid conflicting style names in CSS definitions, even though they might be removed by tailwind-merge or the likes. Make the variants explicit instead by using cva, conditions or lookup tables.

Version History

  • 5b59af4 Current 2026-09-23 09:07

    优化对话框和抽屉控制器的通用状态管理逻辑

  • f6e56cb 2026-08-29 05:20

    新增 Minimal Interface 和 Explicit States 章节,强调无未用 props、避免可选 props 及使用 Pick<> 替代 Omit<>;移除旧版 Composition 中关于纯函数/钩子的部分描述,细化组合逻辑。

  • f7e3c26 2026-08-20 17:47

Same Skill Collection

.agents/skills/add-model-price/SKILL.md
.agents/skills/agent-setup-maintenance/SKILL.md
.agents/skills/analyze-cloud-costs/SKILL.md
.agents/skills/backend-dev-guidelines/SKILL.md
.agents/skills/changelog-writing/SKILL.md
.agents/skills/clickhouse-best-practices/SKILL.md
.agents/skills/code-review/SKILL.md
.agents/skills/create-repo-agent/SKILL.md
.agents/skills/cursor-agents-workflow/SKILL.md
.agents/skills/datadog-query-recipes/SKILL.md
.agents/skills/debug-issue-with-datadog/SKILL.md
.agents/skills/frontend-browser-review/SKILL.md
.agents/skills/frontend-large-feature-architecture/SKILL.md
.agents/skills/git-workflow/SKILL.md
.agents/skills/grill-me/SKILL.md
.agents/skills/housekeeping/SKILL.md
.agents/skills/incident-alert-tickets/SKILL.md
.agents/skills/infra-scaling/SKILL.md
.agents/skills/langfuse-codebase-navigator/SKILL.md
.agents/skills/langfuse-previews/SKILL.md
.agents/skills/linear-agent-writes/SKILL.md
.agents/skills/linear-bug-triage/SKILL.md
.agents/skills/linear-context-handover/SKILL.md
.agents/skills/pnpm-upgrade-package/SKILL.md
.agents/skills/posthog-instrumentation/SKILL.md
.agents/skills/react-component-cleaner/SKILL.md
.agents/skills/refactor-react-effects/SKILL.md
.agents/skills/security-review/SKILL.md
.agents/skills/seed-test-data/SKILL.md
.agents/skills/sentry-instrumentation/SKILL.md
.agents/skills/skill-creator/SKILL.md
.agents/skills/turborepo/SKILL.md
.agents/skills/weekly-production-review/SKILL.md
web/.agents/skills/vercel-composition-patterns/SKILL.md
web/.agents/skills/vercel-react-best-practices/SKILL.md
.agents/skills/linear-work-rhythm/SKILL.md
.agents/skills/storybook/SKILL.md

Metadata

Files
0
Version
5b59af4
Hash
a9ad7800
Indexed
2026-08-20 17:47

trang chủ - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-09-26 13:40
浙ICP备14020137号-1