首页/ AI 技能 / @skilllib/skill-2117939930272411391
Skill站外收录 @skilllib/skill-2117939930272411391

rendering-hoist-jsx

将组件内重复渲染的静态 JSX 元素提取到组件外部定义,实现跨渲染周期复用,显著降低内存分配与虚拟 DOM 创建开销,尤其适用于大型静态 SVG 或列表中频繁出现的不变结构。

0.0 (0) 23 次安装 原作者 编程导航
收录于 2026-01-22
软件开发 AI 技能

Skills.MD

## Hoist Static JSX Elements Extract static JSX outside components to avoid re-creation. **Incorrect (recreates element every render):** ```tsx function LoadingSkeleton() { return <div className="animate-pulse h-20 bg-gray-200" /> } function Container() { return ( <div> {loading && <LoadingSkeleton />} </div> ) } ``` **Correct (reuses same element):** ```tsx const loadingSkeleton = ( <div className="animate-pulse h-20 bg-gray-200" /> ) function Container() { return ( <div> {loading && loadingSkeleton} </div> ) } ``` This is especially helpful for large and static SVG nodes, which can be expensive to recreate on every render. **Note:** If your project has [React Compiler](https://react.dev/learn/react-compiler) enabled, the compiler automatically hoists static JSX elements and optimizes component re-renders, making manual hoisting unnecessary.

评论 0

还没有评论,来抢沙发。