React

作者 lobehub6a3eba96b010无许可证83K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

Use for TSX components, UI libraries, styling, state locality, layout, render performance and memoization.

AI 生成的概览

指导编写 React TSX 组件:样式、组件选择、状态归属、布局与记忆化。

功能
该技能提供一份编写 React TSX 组件的书面指南。内容涵盖样式方案、项目组件与 UI 库之间的组件选用优先级、将状态保持在最小归属者的规则、布局约定,以及何时使用记忆化。它还指向一个布局参考文件和若干相关技能。
适用场景
在编写或重构 React TSX 组件、决定从哪个 UI 组件库导入、判断状态应放在何处,或评估是否需要记忆化时使用。
运行要求
不含脚本,仅为说明文档。它引用 @lobehub/ui、antd 等项目依赖以及一个本地参考文件,因此需要具备这些依赖和相应代码库。

React Component Writing Guide

Styling

ScenarioApproach
Most casescreateStaticStyles + cssVar.* (zero-runtime, module-level)
Simple one-offInline style attribute
Truly dynamic (JS color fns like readableColor/chroma)createStyles + token — last resort

Component Priority

  1. src/components — project-specific reusable components
  2. @lobehub/ui/base-ui — headless primitives. If the component lives here, use it. Do NOT import the same-named root export.
  3. @lobehub/ui — higher-level / antd-wrapping components (only when no base-ui equivalent)
  4. antd — only when neither base-ui nor @lobehub/ui root provides it
  5. Custom implementation — true last resort

If unsure about available components, search existing code or check node_modules/@lobehub/ui/es/index.mjs and node_modules/@lobehub/ui/es/base-ui/.

@lobehub/ui/base-ui — always prefer for these

ComponentImport
Alert (+ AlertProps)import { Alert, type AlertProps } from '@lobehub/ui/base-ui';
Select (+ SelectProps, SelectOption)import { Select } from '@lobehub/ui/base-ui';
Modal (imperative API)import { createModal, confirmModal, useModalContext, type ModalInstance } from '@lobehub/ui/base-ui';
DropdownMenuimport { DropdownMenu } from '@lobehub/ui/base-ui';
ContextMenuimport { ContextMenu } from '@lobehub/ui/base-ui';
Popoverimport { Popover } from '@lobehub/ui/base-ui';
ScrollAreaimport { ScrollArea } from '@lobehub/ui/base-ui';
Switchimport { Switch } from '@lobehub/ui/base-ui';
Toastimport { Toast } from '@lobehub/ui/base-ui';
FloatingSheetimport { FloatingSheet } from '@lobehub/ui/base-ui';
Drawerimport { Drawer } from '@lobehub/ui/base-ui';

For Modal specifically, see the dedicated modal skill — use the imperative createModal({ content: … }) pattern over the legacy <Modal open … /> declarative pattern. base-ui has its own ModalHost already mounted in SPAGlobalProvider.

Common slip: import { Select } from '@lobehub/ui' looks fine but it's the antd-backed Select. Use base-ui Select. Same for Modal, DropdownMenu, etc.

@lobehub/ui root — use when base-ui has no equivalent

CategoryComponents
GeneralActionIcon, ActionIconGroup, Block, Button, Icon
Data DisplayAvatar, Collapse, Empty, Highlighter, Markdown, Tag, Tooltip
Data EntryCodeEditor, CopyButton, EditableText, Form, Input, InputPassword, SearchBar, TextArea
LayoutCenter, DraggablePanel, Flexbox, Grid, Header, MaskShadow
NavigationBurger, Menu, SideNav, Tabs

State

Keep transient state in its smallest useful owner. Extract a custom hook when state transitions and handlers obscure rendering or form a reusable unit; do not extract solely because a component has a particular number of hooks.

Split a component only to establish a real state, reuse, render-update, or mountable-capability boundary. Do not split solely to make files smaller. Decomposing a heavy domain feature into host-assembled atoms is owned by compose-atoms.

Render Performance and Memoization

Treat memo, useMemo, and useCallback as opt-in optimizations, not default component wrappers. Before adding one, identify the actual rerender boundary and prefer structural fixes:

  1. Split at the update boundary.
  2. Move transient state to its smallest owner.
  3. Use narrow Zustand selectors and avoid broad subscriptions.

Do not memoize prop-free or trivially rendered components, or a component that normally receives new objects, arrays, functions, or JSX children. Do not use memoization to compensate for state held too high in the tree.

Use memoization only when the subtree is demonstrably expensive or frequently repeated, its relevant inputs are stable during normal parent renders, and profiling or a concrete render-path analysis identifies the avoided work. State that reason in the implementation summary or PR.

Layout

Use Flexbox and Center from @lobehub/ui. See references/layout-kit.md for full props and examples.

  • Use gap instead of margin for spacing between flex children
  • Use flex={1} to fill available space
  • Nest Flexbox for complex layouts; set overflow: 'auto' for scrollable regions

Related Skills

  • ux: loading visuals (scenario → loader table in references/feedback.md §4.1) and user-facing interaction design.
  • modal: imperative base-ui modal patterns.
  • spa-routes: SPA navigation, route ownership, router configuration, and .desktop variants.
  • compose-atoms: split a heavy domain feature into mountable capability atoms; each host imports only what it mounts.
  • zustand: store structure and selector conventions.

来源与署名

来源:lobehub/lobehub位于.agents/skills/react提交6a3eba9

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架