useMemo
useMemo
ใฏใใฌใณใใผ้ใง่จ็ฎ็ตๆใใญใฃใใทใฅใใใใใฎ React ใใใฏใงใใ
const cachedValue = useMemo(calculateValue, dependencies)
- ใชใใกใฌใณใน
- ไฝฟ็จๆณ
- ใใฉใใซใทใฅใผใใฃใณใฐ
- ๅใฌใณใใผใฎใใณใซ่จ็ฎใ 2 ๅๅฎ่กใใใ
useMemo
ใฎ่ฟใๅคใใใชใใธใงใฏใใงใฏใชใ undefined ใซใชใฃใฆใใพใ- ใณใณใใผใใณใใใฌใณใใผใใใใใณใซ
useMemo
ๅ ใฎ้ขๆฐใๅๅฎ่กใใใ - ใซใผใๅ
ใฎใชในใใฎๅ้
็ฎใซใคใใฆ
useMemo
ใๅผใณๅบใใใใใ็ฆๆญขใใใฆใใ
ใชใใกใฌใณใน
useMemo(calculateValue, dependencies)
ใณใณใใผใใณใใฎใใใใฌใใซใง useMemo
ใๅผใณๅบใใฆใใฌใณใใผ้ใง่จ็ฎใใญใฃใใทใฅใใพใใ
import { useMemo } from 'react';
function TodoList({ todos, tab }) {
const visibleTodos = useMemo(
() => filterTodos(todos, tab),
[todos, tab]
);
// ...
}
ๅผๆฐ
-
calculateValue
: ใญใฃใใทใฅใใใๅคใ่จ็ฎใใ้ขๆฐใ็ด้ขๆฐใงใๅผๆฐใๅใใใไปปๆใฎๅใฎไฝใใใฎๅคใ่ฟใๅฟ ่ฆใใใใพใใReact ใฏๅๅใฌใณใใผไธญใซใใฎ้ขๆฐใๅผใณๅบใใพใใๆฌกๅไปฅ้ใฎใฌใณใใผใงใฏใ็ดๅใฎใฌใณใใผใจdependencies
ใๅคๅใใฆใใชใใใฐใๅใๅคใๅๅบฆ่ฟใใพใใdependencies
ใๅคๅใใฆใใใฐใcalculateValue
ใๅผใณๅบใใฆใใฎ็ตๆใ่ฟใใๅๆใซใๅพใใๅๅฉ็จใใใใใซใใฎ็ตๆใไฟๅญใใพใใ -
dependencies
:calculateValue
ใฎใณใผใๅ ใงๅ็ งใใใฆใใใในใฆใฎใชใขใฏใใฃใๅคใฎ้ ๅใใชใขใฏใใฃใๅคใซใฏใpropsใstateใใใใณใณใณใใผใใณใๆฌไฝใง็ดๆฅๅฎฃ่จใใใฆใใใในใฆใฎๅคๆฐใจ้ขๆฐใๅซใพใใพใใใชใณใฟใ React ๅใใซ่จญๅฎใใใฆใใๅ ดๅใฏใใในใฆใฎใชใขใฏใใฃใๅคใๆญฃใใไพๅญๅคใจใใฆๆๅฎใใใฆใใใใ็ขบ่ชใใพใใไพๅญ้ ๅใฏใ[dep1ใdep2ใdep3]
ใฎใใใซใคใณใฉใคใณใง่จ่ฟฐใใใ้ ๅใฎ้ทใใฏไธๅฎใงใใๅฟ ่ฆใใใใพใใๅไพๅญๅคใฏใObject.is
ใ็จใใฆใๅๅใฎๅคใจๆฏ่ผใใใพใใ
่ฟใๅค
ๅๅใฎใฌใณใใผใงใฏใๅผๆฐใชใใง calculateValue
ใๅผใณๅบใใ็ตๆใใuseMemo
ใฎ่ฟใๅคใจใชใใพใใ
ๆฌกๅไปฅ้ใฎใฌใณใใผใงใฏใไพๅญ้
ๅใๅคๅใใฆใใชใๅ ดๅใฏใไปฅๅใฎใฌใณใใผใงไฟๅญใใใๅคใ่ฟใใพใใๅคๅใใฆใใๅ ดๅใฏใcalculateValue
ใๅๅบฆๅผใณๅบใใใใฎ็ตๆใใใฎใพใพ่ฟใใพใใ
ๆณจๆ็น
useMemo
ใฏใใใฏใชใฎใงใใซในใฟใ ใใใฏใใณใณใใผใใณใใฎใใใใฌใใซใงใใๅผใณๅบใใใจใใงใใพใใใใซใผใใๆกไปถๅๅฒใฎไธญใงๅผใณๅบใใใจใฏใงใใพใใใใใใซใผใใๆกไปถๅๅฒใฎไธญใงๅผใณๅบใใใๅ ดๅใฏใๆฐใใใณใณใใผใใณใใซๅใๅบใใฆใใใฎไธญใซ state ใ็งปๅใใใฆใใ ใใใ- Strict Mode ใงใฏใ็ด็ฒใงใชใ้ขๆฐใ่ฆใคใใใใใใใใใซใ่จ็ฎ้ขๆฐ (
calculateValue
) ใ 2 ๅบฆๅผใณๅบใใใพใใใใใฏใ้็บๆใฎใฟใฎๆๅใงใๆฌ็ชใงใฏๅฝฑ้ฟใฏไธใใพใใใใใใ่จ็ฎ้ขๆฐใ็ด็ฒใงใใใฐ๏ผ็ด็ฒใงใใในใใงใ๏ผใ2 ๅๅผใณๅบใใใฆใใณใผใใซๅฝฑ้ฟใฏใใใพใใใ2 ๅใฎๅผใณๅบใใฎใใกใไธๆนใฎๅผใณๅบใ็ตๆใฏ็ก่ฆใใใพใใ - ็นๅฅใช็็ฑใใชใ้ใใใญใฃใใทใฅใใใๅคใ็ ดๆฃใใใใใจใฏใใใพใใใใญใฃใใทใฅใ็ ดๆฃใใใใฑใผในใฎไพใจใใฆใฏใ้็บๆใซใณใณใใผใใณใใฎใใกใคใซใ็ทจ้ใใๅ ดๅใใใใพใใใพใใ้็บๆใใใณๆฌ็ชๆใซใๅๅใใฆใณใไธญใซใณใณใใผใใณใใใตในใใณใใใใจใใญใฃใใทใฅใฏ็ ดๆฃใใใพใใๅฐๆฅ็ใซใฏใใญใฃใใทใฅใ็ ดๆฃใใใใใจใๅๆใจใใๆฉ่ฝใ React ใซ่ฟฝๅ ใใใๅฏ่ฝๆงใใใใพใใไพใใฐใๅฐๆฅ็ใซไปฎๆณใชในใใ็ตใฟ่พผใฟใงใตใใผใใใใๅ ดๅใไปฎๆณใใผใใซใฎใใฅใผใใผใใใในใฏใญใผใซใขใฆใใใ้
็ฎใฏใใญใฃใใทใฅใ็ ดๆฃใใใใใซใชใใใใใใพใใใใใฎใใใชๆๅใฏใใใใฉใผใใณในๆ้ฉๅใฎใฟใ็ฎ็ใจใใฆ
useMemo
ใไฝฟใฃใฆใใๅ ดๅใซใฏๅ้กใใใพใใใใใใใไปใฎ็ฎ็ใงๅฉ็จใใฆใใๅ ดๅใฏใstate ๅคๆฐ ใ ref ใๅฉ็จใใๆนใ่ฏใใใใใใพใใใ
ไฝฟ็จๆณ
้ซใณในใใชๅ่จ็ฎใ้ฟใใ
่คๆฐใฌใณใใผใ่ทจใใง่จ็ฎใใญใฃใใทใฅใใใซใฏใใณใณใใผใใณใใฎใใใใฌใใซใง useMemo
ใๅผใณๅบใใ่จ็ฎใใฉใใใใพใใ
import { useMemo } from 'react';
function TodoList({ todos, tab, theme }) {
const visibleTodos = useMemo(() => filterTodos(todos, tab), [todos, tab]);
// ...
}
useMemo
ใซใฏใ2 ใคใฎๅผๆฐใๆธกใๅฟ
่ฆใใใใพใใ
() =>
ใฎใใใซใๅผๆฐใๅใใใๆฑใใใ่จ็ฎ็ตๆใ่ฟใ่จ็ฎ้ขๆฐใ- ใณใณใใผใใณใๅ ใซใใๅคใฎใใกใ่จ็ฎ้ขๆฐๅ ใงไฝฟ็จใใใฆใใใในใฆใฎๅคใๅซใใไพๅญ้ ๅใ
ๅๅใฌใณใใผใงใฏใuseMemo
ใใ่ฟใใใๅคใฏใ่จ็ฎ้ขๆฐใๅผใณๅบใใ็ตๆใซใชใใพใใ
ๆฌกๅไปฅ้ใฎใฌใณใใผใงใฏใไปๅใฎใฌใณใใผๆใซๆธกใใไพๅญ้
ๅใจใๅๅใฎใฌใณใใผๆใซๆธกใใไพๅญ้
ๅใๆฏ่ผใใใพใใ๏ผObject.is
ใงๆฏ่ผใใพใใ๏ผไพๅญๅคใฎใใใใๅคๅใใฆใใชใๅ ดๅใuseMemo
ใฏไปฅๅใซ่จ็ฎใใๅคใ่ฟใใพใใๅคๅใใฆใใๅ ดๅใฏใๅๅบฆ่จ็ฎใๅฎ่กใใใๆฐใใๅคใ่ฟใใใพใใ
ใคใพใ useMemo
ใฏใไพๅญ้
ๅใๅคๅใใชใ้ใใ่คๆฐใฎใฌใณใใผใ่ทจใใง่จ็ฎ็ตๆใใญใฃใใทใฅใใพใใ
ใใใๅฝนใซ็ซใคๅ ด้ขใ่ฆใฆใฟใพใใใใ
React ใงใฏ้ๅธธใๅใฌใณใใผใ็บ็ใใใใณใซใใณใณใใผใใณใ้ขๆฐๅ
จไฝใๅๅฎ่กใใใพใใไพใใฐใไปฅไธใฎ TodoList
ใงใstate ใๆดๆฐใใใใใ่ฆชใใๆฐใใ props ใๅใๅใฃใใใใๅ ดๅใfilterTodos
้ขๆฐใๅๅฎ่กใใใพใใ
function TodoList({ todos, tab, theme }) {
const visibleTodos = filterTodos(todos, tab);
// ...
}
ใปใจใใฉใฎ่จ็ฎใฏ้ๅธธใซ้ซ้ใซๅฆ็ใใใใใใไฝใๅ้กใซใชใใใจใฏ้ๅธธใใใพใใใใใใใๅทจๅคงใช้
ๅใใใฃใซใฟใชใณใฐใปๅคๆใใฆใใๅ ดๅใใ้ซใณในใใช่จ็ฎใ่กใฃใฆใใๅ ดๅใซใฏใใใผใฟใๅคใใฃใฆใใชใใใฐใใใใฎ่จ็ฎใในใญใใใใใใชใใงใใใใtodos
ใจ tab
ใฎๅคใๅๅใฎใฌใณใใผๆใจๅใๅ ดๅใๅ
ใปใฉใฎใใใซ่จ็ฎใ useMemo
ใงใฉใใใใใใจใงใไปฅๅใซ่จ็ฎใใ visibleTodos
ใๅๅฉ็จใใใใจใใงใใใฎใงใใ
ใใฎใใใชใญใฃใใทใฅใฎใใจใใใกใขๅใจๅผใณใพใใ
ใใใซๆทฑใ็ฅใ
้ๅธธใๆฐๅใฎใชใใธใงใฏใใฎไฝๆใใซใผใใใใฆใใชใ้ใใ้ซใณในใใช่จ็ฎใซใฏใชใใชใใงใใใใใใๆญฃ็ขบใชๅคๆญใใใใๅ ดๅใฏใใณใณใฝใผใซใญใฐใ่ฟฝๅ ใใฆใใณใผใใฎๅฎ่กใซใใใฃใๆ้ใ่จๆธฌใใใใจใใงใใพใใ
console.time('filter array');
const visibleTodos = filterTodos(todos, tab);
console.timeEnd('filter array');
่จๆธฌใใฆใใใคใณใฟใฉใฏใทใงใณใๅฎ่กใใฆใฟใพใใใ๏ผไพใใฐใinput ่ฆ็ด ใซๆๅญใๅ
ฅๅใใใชใฉ๏ผใใใใจใใณใณใฝใผใซใซใfilter array: 0.15ms
ใจใใใใใชใญใฐใ่กจ็คบใใใพใใใญใฐใฎๆ้ใๅ
จไฝ็ใซใใชใ้ทใ๏ผไพใใฐ 1ms
ไปฅไธ๏ผๅ ดๅใใใฎ่จ็ฎใใกใขๅใใใใจใๆๅนใใใใใพใใใๅฎ้จใจใใฆใ่จ็ฎใ useMemo
ใงใฉใใใใฆใฟใฆใใใฎใคใณใฟใฉใฏใทใงใณใซๅฏพใใๅ่จใฎใญใฐๆ้ใๆธๅฐใใใใฉใใใ็ขบ่ชใใฆใฟใฆใใ ใใใ
console.time('filter array');
const visibleTodos = useMemo(() => {
return filterTodos(todos, tab); // Skipped if todos and tab haven't changed
}, [todos, tab]);
console.timeEnd('filter array');
useMemo
ใฏใๆๅใฎใฌใณใใผใ้ซ้ๅใใใใจใฏใงใใพใใใๆดๆฐใฎๅ ดๅใซ้ใฃใฆใไธ่ฆใชๅฆ็ใในใญใใใใใใจใใงใใพใใ
ใพใใใปใจใใฉใฎๅ ดๅใซใใใชใใไฝฟใฃใฆใใใใทใณใฏใใฆใผใถใฎใใทใณใใ้ซ้ใซๅไฝใใใงใใใใใจใๅฟใใฆใฏใใใพใใใใใฎใใใๆๅณ็ใซๅฆ็้ๅบฆใไฝไธใใใฆใใใฉใผใใณในใใในใใใใฎใ่ฏใใงใใใใไพใใฐ Chrome ใงใฏใCPU Throttling ใชใใทใงใณใๆไพใใใฆใใพใใ
ๅ ใใฆใ้็บๆใซ่กใใใใฉใผใใณใน่จๆธฌใงใฏใ็ใซๆญฃ็ขบใช็ตๆใฏๅพใใใชใใใจใซๆณจๆใใฆใใ ใใใ๏ผไพใใฐ Strict Mode ใใชใณใฎๅ ดๅใๅใณใณใใผใใณใใฏๆฌๆฅ 1 ๅใฌใณใใผใใใใจใใใใ2 ๅใฌใณใใผใใใใใจใใใใพใใ๏ผๆใๆญฃ็ขบใซใใใฉใผใใณในใ่จๆธฌใใใซใฏใๆฌ็ช็จใซใขใใชใใใซใใใใฆใผใถใไฝฟ็จใใใใใชใใใคในใงใในใใใฆใใ ใใใ
ใใใซๆทฑใ็ฅใ
ใใฎใตใคใใฎใใใซใใปใจใใฉใฎใคใณใฟใฉใฏใทใงใณใๅคงใพใใชใใฎ๏ผใใผใธๅ จไฝใใปใฏใทใงใณๅ จไฝใ็ฝฎใๆใใใชใฉ๏ผใงใใๅ ดๅใๅคงๆตใฏใกใขๅใฏไธ่ฆใงใใไธๆนใใใญใผใคใณใฐใจใใฃใฟใฎใใใซใใปใจใใฉใฎใคใณใฟใฉใฏใทใงใณใ็ดฐใใชใใฎ๏ผๅณๅฝขใ็งปๅใใใใชใฉ๏ผใงใใๅ ดๅใใกใขๅใฏ้ๅธธใซๅฝนใซ็ซใคใงใใใใ
useMemo
ใๅฉ็จใใๆ้ฉๅใๅใ็บๆฎใใใฎใฏใไปฅไธใฎใใใชใใปใใฎไธ้จใฎใฑใผในใซ้ใใใพใใ
useMemo
ใง่กใ่จ็ฎใ่ใใ้ ใใใใคใใใฎไพๅญๅคใใปใจใใฉๅคๅใใชใๅ ดๅใ- ่จ็ฎใใๅคใใ
memo
ใงใฉใใใใใใณใณใใผใใณใใฎ props ใซๆธกใๅ ดๅใใใฎๅ ดๅใฏใๅคใๅคๅใใฆใใชใๅ ดๅใซใฏๅใฌใณใใผใในใญใใใใใใงใใใใใกใขๅใใใใจใงใไพๅญๅคใ็ฐใชใๅ ดๅใซใฎใฟใณใณใใผใใณใใๅใฌใณใใผใใใใใจใใงใใพใใ - ใใฎๅคใใใปใใฎใใใฏใฎไพๅญๅคใจใใฆๅฉ็จใใใๅ ดๅใไพใใฐใๅฅใฎ
useMemo
ใฎ่จ็ฎ็ตๆใใใฎๅคใซไพๅญใใฆใใๅ ดๅใใuseEffect
ใใใฎๅคใซไพๅญใใฆใใๅ ดๅใชใฉใงใใ
ใใใใฎใฑใผในไปฅๅคใงใฏใ่จ็ฎใ useMemo
ใงใฉใใใใใใจใซใกใชใใใฏใใใพใใใใใใไธๆนใงใuseMemo
ใงใฉใใใใใใจใซใใๅคงใใชใใกใชใใใใใใพใใใใใฎใใใๅๅฅใฎใฑใผในใ่ใใใซใๅฏ่ฝใช้ใๅ
จใฆใใกใขๅใใใใใซใใฆใใใใผใ ใใใใพใใใใฎใขใใญใผใใฎใใกใชใใใฏใใณใผใใ่ชญใฟใซใใใชใใจใใ็นใงใใใพใใใในใฆใฎใกใขๅใๆๅนใงใใใใใงใฏใใใพใใใไพใใฐใๆฏๅๅคๅใใๅคใ 1 ใคๅญๅจใใใ ใใงใใณใณใใผใใณใๅ
จไฝใฎใกใขๅใ็กๆๅณใซใชใฃใฆใใพใใใจใใใใพใใ
ๅฎ้ใซใฏใๆฌกใฎๅๅใซๅพใใใจใงใใกใขๅใฎๅคใใ็กใใใใจใใงใใพใใ
- ใใใณใณใใผใใณใใไปใฎใณใณใใผใใณใใ่ฆ่ฆ็ใซใฉใใใใๅ ดๅใฏใJSX ใๅญ่ฆ็ด ใจใใฆๅใๅใใใใซใใฆใใ ใใใใใฎใใใซใใใใจใงใใฉใใใผใณใณใใผใใณใใ่ช่บซใฎ state ใๆดๆฐใใใจใใงใใๅญ่ฆ็ด ใๅใฌใณใใผใใๅฟ ่ฆใใชใใจ React ใๅคๆญใงใใพใใ
- ใญใผใซใซใช state ใๅฉ็จใใๅฟ ่ฆไปฅไธใซ state ใใชใใใขใใใใใใจใฏ้ฟใใฆใใ ใใใไพใใฐใใใฉใผใ ใใใใขใคใใ ใใใใผใใใฆใใใใฉใใใใฎใใใชไธๆ็ใช state ใฏใใใชใผใฎใใใใฌใใซใใฐใญใผใใซใช state ใฉใคใใฉใชใซไฟๆใใชใใใใซใใฆใใ ใใใ
- ใฌใณใใผใญใธใใฏใ็ด็ฒใซไฟใคใใใซใใฆใใ ใใใใณใณใใผใใณใใฎๅใฌใณใใผใซใใฃใฆใไฝใๅ้กใ่ตทใใใใ็ฎใซ่ฆใใๅ้กใๅผใ่ตทใใใใใใใๅ ดๅใใใใฏใณใณใใผใใณใใฎใใฐใงใ๏ผ ใกใขๅใใใใฎใงใฏใชใใใใฐใไฟฎๆญฃใใฆใใ ใใใ
- state ใๆดๆฐใใไธ่ฆใชใจใใงใฏใใ้ฟใใฆใใ ใใใReact ใขใใชใฎใใใฉใผใใณในๅ้กใฎใปใจใใฉใฏใใจใใงใฏใๅ ใงใฎ้ฃ้็ใช state ๆดๆฐใซใใฃใฆใณใณใใผใใณใใฎใฌใณใใผใไฝๅบฆใๅผใ่ตทใใใใใใใซ็ใใพใใ
- ใจใใงใฏใใใไธ่ฆใชไพๅญๅคใๅ้คใใฆใใ ใใใไพใใฐใใกใขๅใใไปฃใใใซใใชใใธใงใฏใใ้ขๆฐใใจใใงใฏใใฎไธญใๅคใซ็งปๅใใใใ ใใงใ็ฐกๅใซ่งฃๆฑบใงใใๅ ดๅใใใใพใใ
ใใใงใ็นๅฎใฎใคใณใฟใฉใฏใทใงใณใ้ ใใจๆใใๅ ดๅใฏใReact Developer Tools ใฎใใญใใกใคใฉใๅฉ็จใใฆใใฉใฎใณใณใใผใใณใใงใฎใกใขๅใๆใๆๅนใใ็ขบ่ชใใใใใงใกใขๅใ่กใใพใใใใใใใใฎๅๅใๅฎใใใจใงใใณใณใใผใใณใใฎใใใใฐใ็่งฃใๅฎนๆใซใชใใใใๅธธใซๅๅใซๅพใใใจใใใใใใใพใใ็งใใกใฏใ้ทๆ็ใช่ฆ็นใงใฏใใใใใฎๅ้กใไธๆฐใซ่งฃๆฑบใใใใจใใงใใ็ดฐใใใกใขๅใ่ชๅๅใใๆนๆณใ็ ็ฉถใใฆใใพใใ
ไพ 1/2: useMemo
ใๅฉ็จใใฆๅ่จ็ฎใในใญใใใใ
ใใฎไพใงใฏ filterTodos
ใฎๅฎ่ฃ
ใซใฏใใใฆ้
ๅปถใๅ
ฅใฃใฆใใพใใใใฎใใใใฌใณใใผไธญใซๅผใณๅบใ JavaScript ใฎ้ขๆฐใฎๅฆ็ใ่ใใ้
ใๅ ดๅใซใใฉใใชใใใ็ขบ่ชใงใใพใใใฟใใๅใๆฟใใใใใใผใใๅใๆฟใใฆใฟใฆใใ ใใใ
ใฟใใฎๅใๆฟใใ้
ใๆใใใใใฎใฏใๅใๆฟใใซใใฃใฆใ้
ๅปถใๅ
ฅใฃใฆใใ filterTodos
้ขๆฐใๅๅฎ่กใใใฆใใพใฃใฆใใใใใงใใใใฎๆๅใฏ่ใใฆใฟใใฐๅฝใใๅใงใtab
ใๅคๅใใใฎใชใใ่จ็ฎๅ
จไฝใๅๅฎ่กใใๅฟ
่ฆใใใใฏใใงใใ๏ผใชใ 2 ๅๅฎ่กใใใใฎใๆฐใซใชใๅ ดๅใฏใใใกใใๅ็
งใใฆใใ ใใ๏ผ
ๆฌกใซใใใผใใๅใๆฟใใฆใฟใพใใใใuseMemo
ใใใใใใใงใๆๅณ็ใช้
ๅปถใๅ
ฅใฃใฆใใใซใ้ขใใใใ้ซ้ใซๅไฝใใฆใใพใ๏ผ todos
ใจ tab
๏ผuseMemo
ใฎไพๅญ้
ๅใจใใฆๆธกใใฆใใ๏ผใใๅๅใฎใฌใณใใผๆใใๅคๅใใฆใใชใใใใ้
ๅปถใๅ
ฅใฃใฆใใ filterTodos
ใฎๅผใณๅบใใในใญใใใใใฆใใพใใ
import { useMemo } from 'react'; import { filterTodos } from './utils.js' export default function TodoList({ todos, theme, tab }) { const visibleTodos = useMemo( () => filterTodos(todos, tab), [todos, tab] ); return ( <div className={theme}> <p><b>Note: <code>filterTodos</code> is artificially slowed down!</b></p> <ul> {visibleTodos.map(todo => ( <li key={todo.id}> {todo.completed ? <s>{todo.text}</s> : todo.text } </li> ))} </ul> </div> ); }
ใณใณใใผใใณใใฎๅใฌใณใใผใในใญใใใใ
useMemo
ใฏใๅญใณใณใใผใใณใใฎๅใฌใณใใผใฎใใใฉใผใใณในใๆ้ฉๅใใ้ใซใๅฝนใซ็ซใคใใจใใใใพใใใใใ่ชฌๆใใใใใซใTodoList
ใณใณใใผใใณใใใๅญใณใณใใผใใณใใฎ List
ใฎ props ใจใใฆใvisibleTodos
ใๆธกใใใจใ่ใใพใใ
export default function TodoList({ todos, tab, theme }) {
// ...
return (
<div className={theme}>
<List items={visibleTodos} />
</div>
);
}
props ใงใใ theme
ใๅคๅใใใใจไธ็ฌใขใใชใใใชใผใบใใพใใใ<List />
ใ JSX ใใๅ้คใใใจใ้ซ้ใซๅไฝใใใใใซใชใฃใใฏใใงใใใใชใใกใใใฎ List
ใณใณใใผใใณใใซใฏๆ้ฉๅใใไพกๅคใใใใจใใใใจใงใใ
้ๅธธใใใใณใณใใผใใณใใๅใฌใณใใผใใใใจใใฏใใใฎๅญใณใณใใผใใณใใๅๅธฐ็ใซๅ
จใฆๅใฌใณใใผใใใพใใใใใใTodoList
ใ็ฐใชใ theme
ใฎๅคใงๅใฌใณใใผใใใใจใใList
ใณใณใใผใใณใใไธ็ทใซๅใฌใณใใผใใใ็็ฑใงใใใใฎๅไฝใฏใๅใฌใณใใผใซใใใปใฉๅคใใฎ่จ็ฎใณในใใๅฟ
่ฆใจใใชใใณใณใใผใใณใใซใฏ้ฉใใฆใใพใใใใใใใใๅใฌใณใใผใ้
ใใจๅใใฃใๅ ดๅใฏใList
ใณใณใใผใใณใใ memo
ใงๅฒใใใจใงใไธใใใใ props ใๅๅใฎใฌใณใใผใจๅใใงใใๅ ดๅใซ List
ใฎๅใฌใณใใผใในใญใใใใใใจใใงใใพใใ
import { memo } from 'react';
const List = memo(function List({ items }) {
// ...
});
ใใฎๅคๆดใซใใฃใฆใprops ใฎๅ
จ้
็ฎใๅๅใฎใฌใณใใผใจ็ญใใๅ ดๅใซใฏใList
ใฎๅใฌใณใใผใฏในใญใใใใใใใใซใชใใพใใใใใใ่จ็ฎใฎใญใฃใใทใฅใ้่ฆใซใชใ็็ฑใงใ๏ผ useMemo
ใไฝฟใใใซ visibleTodos
ใฎ่จ็ฎใ่กใใใจใๆณๅใใฆใฟใฆใใ ใใใ
export default function TodoList({ todos, tab, theme }) {
// Every time the theme changes, this will be a different array...
const visibleTodos = filterTodos(todos, tab);
return (
<div className={theme}>
{/* ... so List's props will never be the same, and it will re-render every time */}
<List items={visibleTodos} />
</div>
);
}
ไธ่จใฎไพใงใฏใfilterTodos
้ขๆฐใๆฏๅ็ฐใชใ้
ๅใ็ๆใใพใใ๏ผใใใฏใ{}
ใจใใใชใใธใงใฏใใชใใฉใซใใๆฏๅๆฐใใใชใใธใงใฏใใ็ๆใใใใจใจไผผใฆใใพใใ๏ผ้ๅธธใใใๅ้กใซใชใใใจใฏใใใพใใใใไปๅใฎๅ ดๅใฏใList
ใฎ props ใๆฏๅๅฅใฎๅคใซใชใฃใฆใใพใใพใใใใฎใใใmemo
ใซใใๆ้ฉๅใๆๅณใใชใใชใใชใฃใฆใใพใใฎใงใใใใใงใuseMemo
ใๅฝนใซ็ซใกใพใใ
export default function TodoList({ todos, tab, theme }) {
// Tell React to cache your calculation between re-renders...
const visibleTodos = useMemo(
() => filterTodos(todos, tab),
[todos, tab] // ...so as long as these dependencies don't change...
);
return (
<div className={theme}>
{/* ...List will receive the same props and can skip re-rendering */}
<List items={visibleTodos} />
</div>
);
}
visibleTodos
ใฎ่จ็ฎใ useMemo
ใงใฉใใใใใใจใงใ่คๆฐใฎๅใฌใณใใผใฎ้ใงใใฎ็ตๆใๅใใซใชใใใจใไฟ่จผใงใใพใ๏ผไพๅญ้
ๅใๅคใใใชใ้ใ๏ผใ้ๅธธใ็นๅฅใช็็ฑใใชใใใฐใ่จ็ฎใ useMemo
ใงใฉใใใใๅฟ
่ฆใฏใใใพใใใใใฎไพใงใฏใmemo
ใงๅฒใใใใณใณใใผใใณใใซๅคใๆธกใใฆใใใฌใณใใผใฎในใญใใใใงใใใจใใใใจใใใใฎ็นๅฅใช็็ฑใซใใใใพใใไปใซใ useMemo
ใ่ฟฝๅ ใใๅๆฉใฏใใใคใใใใใใฎใใผใธใง่ฉณใใ่งฃ่ชฌใใฆใใใพใใ
ใใใซๆทฑใ็ฅใ
List
ใ memo
ใงใฉใใใใไปฃใใใซใ<List />
JSX ใใผใ่ชไฝใ useMemo
ใงใฉใใใใฆใๆงใใพใใใ
export default function TodoList({ todos, tab, theme }) {
const visibleTodos = useMemo(() => filterTodos(todos, tab), [todos, tab]);
const children = useMemo(() => <List items={visibleTodos} />, [visibleTodos]);
return (
<div className={theme}>
{children}
</div>
);
}
ๆๅใฏๅใใซใชใใพใใvisibleTodos
ใๅคๅใใฆใใชใๅ ดๅใฏใList
ใฏๅใฌใณใใผใใใพใใใ
<List items={visibleTodos} />
ใฎใใใช JSX ใใผใใฏใ{ type: List, props: { items: visibleTodos } }
ใฎใใใชใชใใธใงใฏใใจๅใใงใใใใฎใชใใธใงใฏใใไฝๆใใใณในใใฏ้ๅธธใซๅฐใใใงใใใReact ใฏใใฎๅ
ๅฎนใๅๅใฎๅ
ๅฎนใจๅใใใฉใใใฏๅใใใพใใใใใฎใใใReact ใฏใใใใฉใซใใง List
ใณใณใใผใใณใใๅใฌใณใใผใใใฎใงใใ
ใใใใReact ใๅๅใฎใฌใณใใผๆใจๅ
จใๅใ JSX ใ่ฆใคใใๅ ดๅใใณใณใใผใใณใใฎๅใฌใณใใผใฏ่กใใพใใใใใใฏใJSX ใใผใใใคใใฅใผใฟใใซ (immutable) ใงใใใใใงใใJSX ใใผใใชใใธใงใฏใใฏๆ้ใ็ต้ใใฆใๅคๅใใใใจใฏใชใใใใๅใฌใณใใผใในใญใใใใฆใใพใฃใฆๅ้กใใใพใใใใใใใใใใๆฉ่ฝใใใซใฏใใใผใใ็ใซๅ
จใๅไธใฎใชใใธใงใฏใใงใใๅฟ
่ฆใใใใใณใผใไธใงๅใใใใซ่ฆใใใ ใใงใฏไธๅๅใงใใใใฎไพใงใฏใuseMemo
ใฎใใใใงใใใผใใๅ
จใๅใใชใใธใงใฏใใจใชใฃใฆใใใฎใงใใ
useMemo
ใไฝฟใฃใฆใJSX ใใผใใๆๅใงใฉใใใใใฎใฏไธไพฟใงใใไพใใฐใๆกไปถไปใใงใฉใใใใใใจใฏใงใใพใใใใใฎใใใ้ๅธธใฏ useMemo
ใง JSX ใใผใใใฉใใใใไปฃใใใซใmemo
ใงใณใณใใผใใณใใใงใฉใใใใพใใ
ไพ 1/2: useMemo
ใจ memo
ใๅฉ็จใใฆๅใฌใณใใผใในใญใใใใ
ใใฎไพใงใฏใList
ใณใณใใผใใณใใซใฏไบบ็บ็ใช้
ๅปถใๅ
ฅใฃใฆใใพใใใใฎใใใใฌใณใใผไธญใซๅผใณๅบใใฆใใ React ใณใณใใผใใณใใ่ใใ้
ใๅ ดๅใฎๆๅใ็ขบ่ชใงใใพใใใฟใใๅคๆดใใใใใใผใใๅใๆฟใใใใใฆใฟใฆใใ ใใใ
ใฟใใฎๅใๆฟใใ้
ใๆใใใฎใฏใ้
ๅปถใๅ
ฅใฃใฆใใ List
ใๅใฌใณใใผใใใฆใใพใฃใฆใใใใใงใใใใใฏ่ใใฆใฟใใฐๅฝ็ถใงใtab
ใๅคๅใใใฎใงใใฆใผใถใฎๆฐใใ้ธๆใ็ป้ขใซๅๆ ใใๅฟ
่ฆใใใใพใใ
ๆฌกใซใใใผใใๅใๆฟใใฆใฟใพใใใใuseMemo
ใจ memo
ใใใใใใใงใไบบ็บ็ใช้
ๅปถใใใใซใ้ขใใใใ้ซ้ใซๅไฝใใฆใใพใ๏ผ visibleItems
้
ๅใๅๅใฎใฌใณใใผๆใใๅคๅใใฆใใชใใใใList
ใฏๅใฌใณใใผใในใญใใใใฆใใพใใvisibleItems
้
ๅใๅคๅใใฆใใชใใฎใฏใtodos
ใจ tab
๏ผuseMemo
ใฎไพๅญ้
ๅใจใใฆๆธกใใฆใใ๏ผใใๅๅใฎใฌใณใใผๆใใๅคๅใใฆใใชใใใใงใใ
import { useMemo } from 'react'; import List from './List.js'; import { filterTodos } from './utils.js' export default function TodoList({ todos, theme, tab }) { const visibleTodos = useMemo( () => filterTodos(todos, tab), [todos, tab] ); return ( <div className={theme}> <p><b>Note: <code>List</code> is artificially slowed down!</b></p> <List items={visibleTodos} /> </div> ); }
ไปใฎใใใฏใซๆธกใไพๅญๅคใใกใขๅใใ
ใใ่จ็ฎใใใณใณใใผใใณใใฎๆฌไฝใง็ดๆฅไฝๆใใใใชใใธใงใฏใใซไพๅญใใฆใใใจใใพใใใใ
function Dropdown({ allItems, text }) {
const searchOptions = { matchMode: 'whole-word', text };
const visibleItems = useMemo(() => {
return searchItems(allItems, searchOptions);
}, [allItems, searchOptions]); // ๐ฉ Caution: Dependency on an object created in the component body
// ...
ใใฎใใใชใชใใธใงใฏใใไพๅญๅคใจใใฆไฝฟใใจใกใขๅใฎๆๅณใใชใใชใฃใฆใใพใใพใใใณใณใใผใใณใใๅใฌใณใใผใใใใจใใใณใณใใผใใณใใฎๆฌไฝใซๅซใพใใใณใผใใฏใในใฆๅๅฎ่กใใใพใใsearchOptions
ใชใใธใงใฏใใไฝๆใใใณใผใใใๆฏๅๅๅฎ่กใใใพใใsearchOptions
ใฏ useMemo
ใฎไพๅญๅคใงใใใๆฏๅ็ฐใชใๅคใจใชใใใใไพๅญๅคใๅคๅใใใจๅคๆญใใใsearchItems
ใๆฏๅๅ่จ็ฎใใใพใใ
ใใใไฟฎๆญฃใใใซใฏใsearchOptions
ใชใใธใงใฏใใไพๅญ้
ๅใซๆธกใๅใซใsearchOptions
ใชใใธใงใฏใ่ชไฝใใกใขๅใใพใใใใ
function Dropdown({ allItems, text }) {
const searchOptions = useMemo(() => {
return { matchMode: 'whole-word', text };
}, [text]); // โ
Only changes when text changes
const visibleItems = useMemo(() => {
return searchItems(allItems, searchOptions);
}, [allItems, searchOptions]); // โ
Only changes when allItems or searchOptions changes
// ...
ไธ่จใฎไพใงใฏใtext
ใๅคๅใใชใใใฐใsearchOptions
ใชใใธใงใฏใใๅคๅใใพใใใใใใใใใใซ่ฏใไฟฎๆญฃๆนๆณใฏใsearchOptions
ใชใใธใงใฏใใฎๅฎฃ่จใ useMemo
ใฎ่จ็ฎ้ขๆฐใฎไธญใซ็งปๅใใใใจใงใใ
function Dropdown({ allItems, text }) {
const visibleItems = useMemo(() => {
const searchOptions = { matchMode: 'whole-word', text };
return searchItems(allItems, searchOptions);
}, [allItems, text]); // โ
Only changes when allItems or text changes
// ...
ใใใงใ่จ็ฎใ็ดๆฅ text
ใซไพๅญใใใใใซใชใใพใใใ๏ผtext
ใฏๆๅญๅใชใฎใงใๆๅณใใใๅคๅใใฆใใพใใใจใฏใใใพใใใ๏ผ
้ขๆฐใใกใขๅใใ
Form
ใณใณใใผใใณใใ memo
ใงใฉใใใใใฆใใใจใใพใใ้ขๆฐใ props ใจใใฆๆธกใใฆใฟใพใใใใ
export default function ProductPage({ productId, referrer }) {
function handleSubmit(orderDetails) {
post('/product/' + productId + '/buy', {
referrer,
orderDetails
});
}
return <Form onSubmit={handleSubmit} />;
}
{}
ใ็ฐใชใใชใใธใงใฏใใ็ๆใใใฎใจๅๆงใซใfunction() {}
ใฎใใใช้ขๆฐๅฎฃ่จใใ() => {}
ใฎใใใช้ขๆฐๅผใใพใใใฌใณใใผใใจใซ็ฐใชใ้ขๆฐใ็ๆใใพใใๆฐใใ้ขๆฐใ็ๆใใใใใจ่ชไฝใฏๅ้กใงใฏใชใใ้ฟใใในใใใจใงใใใใพใใใใใใใForm
ใณใณใใผใใณใใใกใขๅใใใฆใใ็ถๆณใงใฏใForm
ใฎ props ใซๆธกใๅคใๅคใใฃใฆใใชใๅ ดๅใฏ Form
ใฎๅใฌใณใใผใในใญใใใใใใจ่ใใใงใใใใๆฏๅ็ฐใชใๅคใ props ใซใใใจใใกใขๅใฏ็กๆๅณใซใชใฃใฆใใพใใพใใ
useMemo
ใง้ขๆฐใใกใขๅใใๅ ดๅใฏใ่จ็ฎ้ขๆฐใใใใซๅฅใฎ้ขๆฐใ่ฟใๅฟ
่ฆใใใใพใใ
export default function Page({ productId, referrer }) {
const handleSubmit = useMemo(() => {
return (orderDetails) => {
post('/product/' + productId + '/buy', {
referrer,
orderDetails
});
};
}, [productId, referrer]);
return <Form onSubmit={handleSubmit} />;
}
ใชใใ ใไธๆฐๅฅฝใงใใญ๏ผ ้ขๆฐใฎใกใขๅใฏใใใใใใจใชใฎใงใใใๅฐ็จใฎ็ตใฟ่พผใฟใใใฏใๆไพใใใฆใใพใใไฝ่จใช้ขๆฐใฎๅ
ฅใๅญใ้ฟใใใซใฏใuseMemo
ใฎไปฃใใใซ useCallback
ใง้ขๆฐใใฉใใใใพใใใใ
export default function Page({ productId, referrer }) {
const handleSubmit = useCallback((orderDetails) => {
post('/product/' + productId + '/buy', {
referrer,
orderDetails
});
}, [productId, referrer]);
return <Form onSubmit={handleSubmit} />;
}
ไธ่จใฎ 2 ใคใฎไพใฏๅฎๅ
จใซ็ญไพกใงใใuseCallback
ใฎใกใชใใใฏใไฝ่จใช้ขๆฐใฎๅ
ฅใๅญใไธ่ฆใซใชใใใจใ ใใงใใใใไปฅๅคใฎ้ใใฏไฝใใใใพใใใuseCallback
ใซใคใใฆใฎ่ฉณ็ดฐใฏใใใกใใๅ็
งใใฆใใ ใใใ
ใใฉใใซใทใฅใผใใฃใณใฐ
ๅใฌใณใใผใฎใใณใซ่จ็ฎใ 2 ๅๅฎ่กใใใ
Strict Mode ใงใฏใๆฌๆฅ 1 ๅใ ใ้ขๆฐใๅผใณๅบใใใใจใใใงใ2 ๅๅผใณๅบใใใใใจใใใใพใใ
function TodoList({ todos, tab }) {
// This component function will run twice for every render.
const visibleTodos = useMemo(() => {
// This calculation will run twice if any of the dependencies change.
return filterTodos(todos, tab);
}, [todos, tab]);
// ...
ใใใฏๆณๅฎ้ใใฎๆๅใงใใใใใใงใณใผใใๅฃใใใใจใใใฃใฆใฏใใใพใใใ
ใใใฏ้็บๆใฎใฟใฎๆๅใงใ้็บ่ ใใณใณใใผใใณใใ็ด็ฒใซไฟใคใใใซๅฝน็ซใกใพใใๅผใณๅบใ็ตๆใฎใใกใฎ 1 ใคใๆก็จใใใใใ 1 ใคใฏ็ก่ฆใใใพใใใใชใใๅฎ่ฃ ใใใณใณใใผใใณใใจ่จ็ฎ้ขๆฐใ็ด็ฒใงใใใฐใใใฎๆๅใใญใธใใฏใซๅฝฑ้ฟใไธใใใใจใฏใใใพใใใใใใใใใๆๅณใใ็ด็ฒใงใฏใชใ้ขๆฐใซใชใฃใฆใใๅ ดๅใฏใใใฎๆๅใซใใฃใฆ้้ใใซๆฐใฅใใไฟฎๆญฃใใใใจใใงใใพใใ
ใใจใใฐใไปฅไธใฎ่จ็ฎ้ขๆฐใฏใprops ใจใใฆๅใๅใฃใ้ ๅใฎๆธใๆใ๏ผใใฅใผใใผใทใงใณ๏ผใใใฆใใพใฃใฆใใใ็ด็ฒใงใฏใใใพใใใ
const visibleTodos = useMemo(() => {
// ๐ฉ Mistake: mutating a prop
todos.push({ id: 'last', text: 'Go for a walk!' });
const filtered = filterTodos(todos, tab);
return filtered;
}, [todos, tab]);
ใใใใใใฎ้ขๆฐใฏ 2 ๅบฆๅผใณๅบใใใใใใtodo ใ 2 ๅ่ฟฝๅ ใใใใใจใซๆฐใฅใใฏใใงใใ่จ็ฎ้ขๆฐใฏใๆขๅญใฎใชใใธใงใฏใใๅคๆดใใฆใฏใใใพใใใใ่จ็ฎไธญใซไฝๆใใๆฐใใใชใใธใงใฏใใๅคๆดใใใใจใฏๅ้กใใใพใใใใใจใใฐใfilterTodos
้ขๆฐใๅธธใซ็ฐใชใ้
ๅใ่ฟใๅ ดๅใฏใใใฎ้
ๅใๅคๆดใใฆใๅ้กใใใพใใใ
const visibleTodos = useMemo(() => {
const filtered = filterTodos(todos, tab);
// โ
Correct: mutating an object you created during the calculation
filtered.push({ id: 'last', text: 'Go for a walk!' });
return filtered;
}, [todos, tab]);
็ด้ขๆฐใซใคใใฆ่ฉณใใ็ฅใใซใฏใใณใณใใผใใณใใ็ด็ฒใซไฟใคใๅ็ งใใฆใใ ใใใ
ใพใใใใฅใผใใผใทใงใณใชใใงใชใใธใงใฏใใๆดๆฐใใๆนๆณใซใคใใฆใฏใชใใธใงใฏใใฎๆดๆฐใใใใฅใผใใผใทใงใณใชใใง้ ๅใๆดๆฐใใๆนๆณใซใคใใฆใฏ้ ๅใฎๆดๆฐใๅ็ งใใฆใใ ใใใ
useMemo
ใฎ่ฟใๅคใใใชใใธใงใฏใใงใฏใชใ undefined ใซใชใฃใฆใใพใ
ไปฅไธใฎใณใผใใฏใใพใๅไฝใใพใใใ
// ๐ด You can't return an object from an arrow function with () => {
const searchOptions = useMemo(() => {
matchMode: 'whole-word',
text: text
}, [text]);
JavaScript ใงใฏใ() => {
ใจใใใณใผใใงใขใญใผ้ขๆฐใฎๆฌไฝใ้ๅงใใใใใ{
ใฎๆณขๆฌๅผงใฏใชใใธใงใฏใใฎไธ้จใซใฏใชใใพใใใใใใใฃใฆใชใใธใงใฏใใฏ่ฟใใใใใในใซใคใชใใใพใใ({
ใ })
ใฎใใใซไธธๆฌๅผงใ่ฟฝๅ ใใใใจใงไฟฎๆญฃใงใใพใใ
// This works, but is easy for someone to break again
const searchOptions = useMemo(() => ({
matchMode: 'whole-word',
text: text
}), [text]);
ใใใใใใใงใใพใ ๆททไนฑใใใใใ่ชฐใใไธธๆฌๅผงใๅ้คใใฆใใพใใจ็ฐกๅใซๅฃใใฆใใพใใพใใ
ใใฎใในใ้ฟใใใใใซใๆ็คบ็ใซ return
ๆใๆธใใพใใใใ
// โ
This works and is explicit
const searchOptions = useMemo(() => {
return {
matchMode: 'whole-word',
text: text
};
}, [text]);
ใณใณใใผใใณใใใฌใณใใผใใใใใณใซ useMemo
ๅ
ใฎ้ขๆฐใๅๅฎ่กใใใ
็ฌฌ 2 ๅผๆฐใซไพๅญ้ ๅใๆๅฎใใฆใใใ็ขบ่ชใใฆใใ ใใ๏ผ
ไพๅญ้
ๅใๅฟใใใจใuseMemo
ใฏๆฏๅ่จ็ฎใๅๅฎ่กใใฆใใพใใพใใ
function TodoList({ todos, tab }) {
// ๐ด Recalculates every time: no dependency array
const visibleTodos = useMemo(() => filterTodos(todos, tab));
// ...
็ฌฌ 2 ๅผๆฐใซไพๅญ้ ๅใๆธกใใไฟฎๆญฃ็ใฏไปฅไธใฎ้ใใงใใ
function TodoList({ todos, tab }) {
// โ
Does not recalculate unnecessarily
const visibleTodos = useMemo(() => filterTodos(todos, tab), [todos, tab]);
// ...
ใใใง่งฃๆฑบใใชใๅ ดๅใฏใๅฐใชใใจใ 1 ใคใฎไพๅญๅคใๅๅใฎใฌใณใใผใจ็ฐใชใฃใฆใใใใจใๅ้กใงใใๆๅใงไพๅญๅคใใณใณใฝใผใซใซๅบๅใใฆใใใใใฐใใใใจใใงใใพใใ
const visibleTodos = useMemo(() => filterTodos(todos, tab), [todos, tab]);
console.log([todos, tab]);
ใณใณใฝใผใซไธใงใๅฅใ
ใฎๅใฌใณใใผใซใใฃใฆ่กจ็คบใใใ 2 ใคใฎ้
ๅใ้ธใณใพใใใใใใใซใคใใฆใ้
ๅใๅณใฏใชใใฏใใโStore as a global variable๏ผใฐใญใผใใซๅคๆฐใจใใฆไฟๅญ๏ผโ ใ้ธๆใใใใจใงใ้
ๅใไฟๅญใใใใจใใงใใพใใ1 ๅ็ฎใซไฟๅญใใ้
ๅใ temp1
ใ2 ๅ็ฎใซไฟๅญใใ้
ๅใ temp2
ใจใใฆไฟๅญใใใใจใใใจใใใฉใฆใถใฎใณใณใฝใผใซใไฝฟ็จใใฆใไธกๆนใฎ้
ๅใฎๅไพๅญๅคใๅใใใฉใใใ็ขบ่ชใงใใพใใ
Object.is(temp1[0], temp2[0]); // Is the first dependency the same between the arrays?
Object.is(temp1[1], temp2[1]); // Is the second dependency the same between the arrays?
Object.is(temp1[2], temp2[2]); // ... and so on for every dependency ...
ใกใขๅใๅฆจใใฆใใไพๅญๅคใ่ฆใคใใใใใใฎไพๅญๅคใๅ้คใใๆนๆณใๆขใใใใใฎไพๅญๅคใใกใขๅใใพใใใใ
ใซใผใๅ
ใฎใชในใใฎๅ้
็ฎใซใคใใฆ useMemo
ใๅผใณๅบใใใใใ็ฆๆญขใใใฆใใ
Chart
ใณใณใใผใใณใใ memo
ใงใฉใใใใใฆใใใจใใพใใReportList
ใณใณใใผใใณใใๅใฌใณใใผใใใๅ ดๅใงใใใชในใๅ
ใฎๅ Chart
ใฎๅใฌใณใใผใฏในใญใใใใใใงใใใจใใใใไปฅไธใฎใใใซใซใผใๅ
ใง useMemo
ใๅผใณๅบใใใจใฏใงใใพใใใ
function ReportList({ items }) {
return (
<article>
{items.map(item => {
// ๐ด You can't call useMemo in a loop like this:
const data = useMemo(() => calculateReport(item), [item]);
return (
<figure key={item.id}>
<Chart data={data} />
</figure>
);
})}
</article>
);
}
ใใฎๅ ดๅใฏใๅใขใคใใ ใใณใณใใผใใณใใซๅใๅบใใใขใคใใ ใใจใซใใผใฟใใกใขๅใใพใใ
function ReportList({ items }) {
return (
<article>
{items.map(item =>
<Report key={item.id} item={item} />
)}
</article>
);
}
function Report({ item }) {
// โ
Call useMemo at the top level:
const data = useMemo(() => calculateReport(item), [item]);
return (
<figure>
<Chart data={data} />
</figure>
);
}
ใใใใฏใuseMemo
ใๅ้คใใReport
่ชไฝใ memo
ใงใฉใใใใใใจใงใ่งฃๆฑบใงใใพใใitem
ใๅคๅใใชใๅ ดๅใฏใReport
ใฎๅใฌใณใใผใฏในใญใใใใใChart
ใฎๅใฌใณใใผใในใญใใใใใพใใ
function ReportList({ items }) {
// ...
}
const Report = memo(function Report({ item }) {
const data = calculateReport(item);
return (
<figure>
<Chart data={data} />
</figure>
);
});