React

From lobehub/lobehub · View interactive version

React is an AI agent skill from the GitHub repository lobehub/lobehub. Use for TSX components, UI libraries, styling, state locality, layout, render performance and memoization.

How to use this skill

Skill file

React Component Writing Guide

Styling

| Scenario | Approach |

| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

| Files already on StyleX, and new SPA styles | stylex.create + @lobehub/ui/stylex/cssVar.stylex / @lobehub/ui/stylex/media.stylex. The SPA compiler is plugins/vite/stylex.ts (@stylexjs/unplugin, prefix lh). Do not copy those token modules into the app. |

| Existing styles not yet migrated | createStaticStyles + cssVar.* (zero-runtime, module-level) |

| Simple one-off | Inline style attribute |

| Truly dynamic (JS color fns like readableColor/chroma) | createStyles + token — last resort |

StyleX

1. The component's slot props. lobe-ui components take classNames / styles per part, e.g. Tabs → classNames={{ list: stylex.props(styles.tabList).className, tab: stylex.props(styles.tab).className }}.

2. A pseudo-class on the slot's element: StyleX compiles keys like ':has([data-replace-all])' and ':nth-child(2)', so .tabs [role=tab]:has([data-replace-all]) becomes classNames.tab with marginInlineStart: { default: null, ':has([data-replace-all])': 'auto' }.

3. Our own component: add an xstyle?: StyleXStyles prop (or <part>Xstyle for an inner part) and apply it there.

4. Only DOM no prop can reach (editor content such as .ProseMirror p, rendered markdown, third-party widgets, lobe-ui parts without a slot) keeps its original nested rule, unchanged, in a createStaticStyles object in the same file, merged onto the host element with {...sx(styles.content, fallback.content)}. Put // oxlint-disable-next-line stylex-migration/no-emotion-styles -- <what no prop reaches> above the @lobehub/ui import. Add a missing slot to lobe-ui instead when the component is ours.

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

| Component | Import |

| ------------------------------------------ | ------------------------------------------------------------------------------------------------------- |

| 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'; |

| DropdownMenu | import { DropdownMenu } from '@lobehub/ui/base-ui'; |

| ContextMenu | import { ContextMenu } from '@lobehub/ui/base-ui'; |

| Popover | import { Popover } from '@lobehub/ui/base-ui'; |

| ScrollArea | import { ScrollArea } from '@lobehub/ui/base-ui'; |

| Switch | import { Switch } from '@lobehub/ui/base-ui'; |

| Toast | import { Toast } from '@lobehub/ui/base-ui'; |

| FloatingSheet | import { FloatingSheet } from '@lobehub/ui/base-ui'; |

| Drawer | import { 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

| Category | Components |

| ------------ | ------------------------------------------------------------------------------------- |

| General | ActionIcon, ActionIconGroup, Block, Button, Icon |

| Data Display | Avatar, Collapse, Empty, Highlighter, Markdown, Tag, Tooltip |

| Data Entry | CodeEditor, CopyButton, EditableText, Form, Input, InputPassword, SearchBar, TextArea |

| Layout | Center, DraggablePanel, Flexbox, Grid, Header, MaskShadow |

| Navigation | Burger, 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; when a file reaches the 1200-line limit in source-layout, find one of these boundaries rather than cutting at a line count. Decomposing a heavy domain feature into host-assembled atoms is owned by compose-atoms.

Popover Exits

An action inside popover content that leaves the popover must close it, or the popover stays over the destination (for example, a plan badge in the user panel navigating to /settings/plans).

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.

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.