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
ใๅๅฉ็จใใใใจใใงใใใฎใงใใ
ใใฎใใใชใญใฃใใทใฅใฎใใจใใmemoization ๏ผใกใขๅ๏ผ ใจๅผใณใพใใ
ใใใซๆทฑใ็ฅใ
้ๅธธใๆฐๅใฎใชใใธใงใฏใใฎไฝๆใใซใผใใใใฆใใชใ้ใใ้ซใณในใใช่จ็ฎใซใฏใชใใชใใงใใใใใใๆญฃ็ขบใชๅคๆญใใใใๅ ดๅใฏใใณใณใฝใผใซใญใฐใ่ฟฝๅ ใใฆใใณใผใใฎๅฎ่กใซใใใฃใๆ้ใ่จๆธฌใใใใจใใงใใพใใ
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 ใขใใชใฎใใใฉใผใใณในๅ้กใฎใปใจใใฉใฏใใจใใงใฏใใใ็บ็ใใๆดๆฐใฎ้ฃ้ใซใใฃใฆๅผใ่ตทใใใใพใใใใฎ้ฃ้ใซใใฃใฆใใณใณใใผใใณใใไฝๅบฆใ็นฐใ่ฟใใฌใณใใผใใใฆใใพใใฎใงใใ
- ใจใใงใฏใใใไธ่ฆใชไพๅญๅคใๅ้คใใฆใใ ใใใไพใใฐใใกใขๅใใไปฃใใใซใใชใใธใงใฏใใ้ขๆฐใใจใใงใฏใใฎไธญใๅคใซ็งปๅใใใใ ใใงใ็ฐกๅใซ่งฃๆฑบใงใใๅ ดๅใใใใพใใ
ใใใงใ็นๅฎใฎใคใณใฟใฉใฏใทใงใณใ้ ใใจๆใใๅ ดๅใฏใ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>
);
}
็งใใกใฏใtheme
props ใๅคๅใใใใจใไธๆ็ใซใขใใชใใใชใผใบใใใใจใ็ขบ่ชใใพใใใใใใใ<List />
ใ JSX ใใๅ้คใใใจใ้ซ้ใซๅไฝใใใใใซ่ฆใใพใใ
้ๅธธใใใใณใณใใผใใณใใๅใฌใณใใผใใใใจใใฏใใใฎๅญใณใณใใผใใณใใๅ
จใฆๅใฌใณใใผใใใพใใใใใใ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 }) {
// ใใใฏใtheme ใๅคๅใใใใณใซๆฐใใ้
ๅใซใชใ...
const visibleTodos = filterTodos(todos, tab);
return (
<div className={theme}>
{/* ... ใใฎใใใList ใฎ props ใๅใๅคใซใชใใใจใฏใชใใๆฏๅๅใฌใณใใผใใใใใจใซใชใ */}
<List items={visibleTodos} />
</div>
);
}
ไธ่จใฎไพใงใฏใfilterTodos
้ขๆฐใๆฏๅ็ฐใชใ้
ๅใ็ๆใใพใใ๏ผใใใฏใ{}
ใจใใใชใใธใงใฏใใชใใฉใซใใๆฏๅๆฐใใใชใใธใงใฏใใ็ๆใใใใจใจไผผใฆใใพใใ๏ผ้ๅธธใใใๅ้กใซใชใใใจใฏใใใพใใใใไปๅใฎๅ ดๅใฏใList
ใฎ props ใๆฏๅๅฅใฎๅคใซใชใฃใฆใใพใใพใใใใฎใใใmemo
ใซใใๆ้ฉๅใๆๅณใใชใใชใใชใฃใฆใใพใใฎใงใใใใใงใuseMemo
ใๅฝนใซ็ซใกใพใใ
export default function TodoList({ todos, tab, theme }) {
// Reactใซใๅใฌใณใใผ้ใง่จ็ฎ็ตๆใใญใฃใใทใฅใใใใใซไผใใพใใ...
const visibleTodos = useMemo(
() => filterTodos(todos, tab),
[todos, tab] // ...ใใใใฎไพๅญๅคใๅคๅใใชใ้ใใฏใ
);
return (
<div className={theme}>
{/* ... List ใฏๅใ props ใๅใๅใใๅใฌใณใใผใในใญใใใใพใ */}
<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 ใใผใใไธๅคใงใใใใใงใใ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]); // ๐ฉ ๆณจๆ: ใณใณใใผใใณใใฎๆฌไฝใงไฝๆใใใใชใใธใงใฏใใธใฎไพๅญๅค
// ...
ใใฎใใใชใชใใธใงใฏใใซไพๅญใใฆใใพใใจใใกใขๅใใใพใๅใใพใใใใณใณใใผใใณใใๅใฌใณใใผใใใใจใใใณใณใใผใใณใใฎๆฌไฝใซๅซใพใใใณใผใใฏๅ
จใฆๅๅฎ่กใใใพใใsearchOptions
ใชใใธใงใฏใใไฝๆใใใณใผใใใๆฏๅๅๅฎ่กใใใพใใsearchOptions
ใฏ useMemo
ใฎไพๅญๅคใงใใใๆฏๅ็ฐใชใๅคใจใชใใใใไพๅญๅคใๅคๅใใใจๅคๆญใใใsearchItems
ใๆฏๅๅ่จ็ฎใใใพใใ
ใใใไฟฎๆญฃใใใซใฏใsearchOptions
ใชใใธใงใฏใใไพๅญ้
ๅใซๆธกใๅใซใsearchOptions
ใชใใธใงใฏใ่ชไฝใใกใขๅใใพใใใใ
function Dropdown({ allItems, text }) {
const searchOptions = useMemo(() => {
return { matchMode: 'whole-word', text };
}, [text]); // โ
text ใๅคๅใใใจใใ ใๆดๆฐใใใ
const visibleItems = useMemo(() => {
return searchItems(allItems, searchOptions);
}, [allItems, searchOptions]); // โ
allItems ใ searchOptions ใๅคๅใใใจใใ ใๆดๆฐใใใ
// ...
ไธ่จใฎไพใงใฏใtext
ใๅคๅใใชใใใฐใsearchOptions
ใชใใธใงใฏใใๅคๅใใพใใใใใใใใใใซ่ฏใไฟฎๆญฃๆนๆณใจใใฆใsearchOptions
ใชใใธใงใฏใใฎๅฎฃ่จใ useMemo
ใฎ่จ็ฎ้ขๆฐใฎไธญใซ็งปๅใใๆนๆณใใใใพใใ
function Dropdown({ allItems, text }) {
const visibleItems = useMemo(() => {
const searchOptions = { matchMode: 'whole-word', text };
return searchItems(allItems, searchOptions);
}, [allItems, text]); // โ
allItems ใ text ใๅคๅใใใจใใ ใๆดๆฐใใใ
// ...
ใใใงใ่จ็ฎใ็ดๆฅ 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 }) {
// ใใฎใณใณใใผใใณใ้ขๆฐใฏใใฌใณใใผใใจใซ 2 ๅๅฎ่กใใใพใใ
const visibleTodos = useMemo(() => {
// ใใฎ่จ็ฎใฏใไพๅญๅคใฎใใใใใๅคๅใใๅ ดๅใซ 2 ๅๅฎ่กใใใพใใ
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);
// โ
ๆญฃใใ: ่จ็ฎไธญใซไฝๆใใใชใใธใงใฏใใๅคๆดใใฆใใ
filtered.push({ id: 'last', text: 'Go for a walk!' });
return filtered;
}, [todos, tab]);
็ด็ฒๆงใซใคใใฆ่ฉณใใ็ฅใใซใฏใใณใณใใผใใณใใ็ด็ฒใซไฟใคใๅ็ งใใฆใใ ใใใ
ใพใใใใฅใผใใผใทใงใณใชใใงใชใใธใงใฏใใๆดๆฐใใๆนๆณใซใคใใฆใฏใชใใธใงใฏใใฎๆดๆฐใใใใฅใผใใผใทใงใณใชใใง้ ๅใๆดๆฐใใๆนๆณใซใคใใฆใฏ้ ๅใฎๆดๆฐใๅ็ งใใฆใใ ใใใ
useMemo
ใฎ่ฟใๅคใใใชใใธใงใฏใใงใฏใชใ undefined ใซใชใฃใฆใใพใ
ไปฅไธใฎใณใผใใฏใใพใๅไฝใใพใใใ
// ๐ด ใขใญใผ้ขๆฐใใใชใใธใงใฏใใ่ฟใใซใฏใ`() => {` ใฏๅฉ็จใงใใชใ
const searchOptions = useMemo(() => {
matchMode: 'whole-word',
text: text
}, [text]);
JavaScript ใงใฏใ() => {
ใจใใใณใผใใงใขใญใผ้ขๆฐใฎๆฌไฝใ้ๅงใใใใใ{
ใฎๆณขๆฌๅผงใฏใชใใธใงใฏใใฎไธ้จใซใฏใชใใพใใใใใใใฃใฆใชใใธใงใฏใใฏ่ฟใใใใใในใซใคใชใใใพใใ({
ใ })
ใฎใใใซไธธๆฌๅผงใ่ฟฝๅ ใใใใจใงไฟฎๆญฃใงใใพใใ
// ใใใฏๆญฃใใๅไฝใใพใใใ่ชฐใใใพใๅฃใใฆใใพใๅฏ่ฝๆงใใใ
const searchOptions = useMemo(() => ({
matchMode: 'whole-word',
text: text
}), [text]);
ใใใใใใใงใใพใ ๆททไนฑใใใใใ่ชฐใใไธธๆฌๅผงใๅ้คใใฆใใพใใจ็ฐกๅใซๅฃใใฆใใพใใพใใ
ใใฎใในใ้ฟใใใใใซใๆ็คบ็ใซ return
ๆใๆธใใพใใใใ
// โ
ใใใฏๆญฃใใๅไฝใใๆ็คบ็ใซๆธใใใฆใใ
const searchOptions = useMemo(() => {
return {
matchMode: 'whole-word',
text: text
};
}, [text]);
ใณใณใใผใใณใใใฌใณใใผใใใใใณใซใuseMemo
ๅ
ใฎ้ขๆฐใๅๅฎ่กใใใ
็ฌฌ 2 ๅผๆฐใซไพๅญ้ ๅใๆๅฎใใฆใใใ็ขบ่ชใใฆใใ ใใ๏ผ
ไพๅญ้
ๅใๅฟใใใจใuseMemo
ใฏๆฏๅ่จ็ฎใๅๅฎ่กใใฆใใพใใพใใ
function TodoList({ todos, tab }) {
// ๐ด ไพๅญ้
ๅใใชใๅ ดๅใๆฏๅๅ่จ็ฎใ่กใ
const visibleTodos = useMemo(() => filterTodos(todos, tab));
// ...
็ฌฌ 2 ๅผๆฐใซไพๅญ้ ๅใๆธกใใไฟฎๆญฃ็ใฏไปฅไธใฎ้ใใงใใ
function TodoList({ todos, tab }) {
// โ
ๅฟ
่ฆใชๅ ดๅใใๅ่จ็ฎใ่กใใชใ
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]); // 1 ใค็ฎใฎไพๅญๅคใ้
ๅ้ใงๅใใ
Object.is(temp1[1], temp2[1]); // 2 ใค็ฎใฎไพๅญๅคใ้
ๅ้ใงๅใใ
Object.is(temp1[2], temp2[2]); // ... ๅ
จใฆใฎไพๅญๅคใซใคใใฆ็นฐใ่ฟใ ...
ใกใขๅใๅฆจใใฆใใไพๅญๅคใ่ฆใคใใใใใใฎไพๅญๅคใๅ้คใใๆนๆณใๆขใใใใใฎไพๅญๅคใใกใขๅใใพใใใใ
ใซใผใๅ
ใฎใชในใใฎๅ้
็ฎใซใคใใฆ useMemo
ใๅผใณๅบใใใใใ็ฆๆญขใใใฆใใ
Chart
ใณใณใใผใใณใใ memo
ใงใฉใใใใใฆใใใจใใพใใReportList
ใณใณใใผใใณใใๅใฌใณใใผใใใๅ ดๅใงใใใชในใๅ
ใฎๅ Chart
ใฎๅใฌใณใใผใฏในใญใใใใใใงใใใจใใใใไปฅไธใฎใใใซใซใผใๅ
ใง useMemo
ใๅผใณๅบใใใจใฏใงใใพใใใ
function ReportList({ items }) {
return (
<article>
{items.map(item => {
// ๐ด ใใฎใใใซใใซใผใใฎไธญใง useMemo ใๅผใณๅบใใใจใฏใงใใชใ
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 }) {
// โ
useMemo ใใใใใฌใใซใงๅผใณๅบใใใจใใงใใ
const data = useMemo(() => calculateReport(item), [item]);
return (
<figure>
<Chart data={data} />
</figure>
);
}
ใใใใฏใuseMemo
ใๅ้คใใReport
่ชไฝใ memo
ใงใฉใใใใใใจใงใ่งฃๆฑบใงใใพใใitem
props ใๅคๅใใชใๅ ดๅใฏใReport
ใฎๅใฌใณใใผใฏในใญใใใใใChart
ใฎๅใฌใณใใผใในใญใใใใใพใใ
function ReportList({ items }) {
// ...
}
const Report = memo(function Report({ item }) {
const data = calculateReport(item);
return (
<figure>
<Chart data={data} />
</figure>
);
});