fb79a15508
- 添加 sections 定义文件,包含性能优化各领域分类 - 添加规则模板文件,规范文档结构和标签定义 - 添加异步操作优化规则,包括防止瀑布流、并行化、延迟等待等 - 添加包大小优化规则,包括避免桶式导入、动态导入、预加载等 - 添加服务端性能优化规则,包括 API 路
36 lines
1018 B
Markdown
36 lines
1018 B
Markdown
---
|
|
title: Do not wrap a simple expression with a primitive result type in useMemo
|
|
impact: LOW-MEDIUM
|
|
impactDescription: wasted computation on every render
|
|
tags: rerender, useMemo, optimization
|
|
---
|
|
|
|
## Do not wrap a simple expression with a primitive result type in useMemo
|
|
|
|
When an expression is simple (few logical or arithmetical operators) and has a primitive result type (boolean, number, string), do not wrap it in `useMemo`.
|
|
Calling `useMemo` and comparing hook dependencies may consume more resources than the expression itself.
|
|
|
|
**Incorrect:**
|
|
|
|
```tsx
|
|
function Header({ user, notifications }: Props) {
|
|
const isLoading = useMemo(() => {
|
|
return user.isLoading || notifications.isLoading
|
|
}, [user.isLoading, notifications.isLoading])
|
|
|
|
if (isLoading) return <Skeleton />
|
|
// return some markup
|
|
}
|
|
```
|
|
|
|
**Correct:**
|
|
|
|
```tsx
|
|
function Header({ user, notifications }: Props) {
|
|
const isLoading = user.isLoading || notifications.isLoading
|
|
|
|
if (isLoading) return <Skeleton />
|
|
// return some markup
|
|
}
|
|
```
|