Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
React Hooks を安全に使う
Search
kobayang
May 27, 2019
Technology
1.4k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
React Hooks を安全に使う
kobayang
May 27, 2019
More Decks by kobayang
See All by kobayang
React Suspenseを使って遷移体験を向上させる
kobayang
4
2k
Apollo Client Cache
kobayang
0
1.3k
Puppeteerを導入してみた話
kobayang
2
150k
ゼロから学ぶWeb Authentication API
kobayang
0
1.3k
react-beautiful-dnd を使いたかった話
kobayang
2
2k
Other Decks in Technology
See All in Technology
新しいVibe Codingと”自走”について
watany
5
290
protovalidate-es を導入してみた
bengo4com
0
170
現地で盛り上がった WWDC26 Keynote
zozotech
PRO
1
170
Android の公式 Skill / Android skills
yanzm
0
120
2026TECHFRESH畢業分享會 - Lightning Talk - 資料也要 CI/CD? 用 Airbyte 自動化資料同步
line_developers_tw
PRO
0
700
Socrates × Looker 〜セマンティックレイヤーで進化するデータ分析エージェント〜
hanon52_
3
2.1k
Snowflakeと仲良くなる第一歩
coco_se
4
410
AIっぽい文章を採点して人間らしく直すアプリを作ってみた
yama3133
2
120
攻撃者視点で考えるDetection Engineering
cryptopeg
0
840
自律型AIエージェントは何を破壊するのか
kojira
0
150
FDE という解 ― 暗黙知と明示知をつなぐ、伴走型エンジニアリング ―
otanet
0
130
2026TECHFRESH畢業分享會 - 原生還是跨平台? App 開發踩坑實錄
line_developers_tw
PRO
0
720
Featured
See All Featured
Six Lessons from altMBA
skipperchong
29
4.3k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
610
So, you think you're a good person
axbom
PRO
2
2.1k
Designing for Timeless Needs
cassininazir
1
250
Faster Mobile Websites
deanohume
310
31k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
1.7k
How STYLIGHT went responsive
nonsquared
100
6.2k
Fireside Chat
paigeccino
42
3.9k
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Building Applications with DynamoDB
mza
96
7.1k
Everyday Curiosity
cassininazir
0
230
Transcript
©2018 Wantedly, Inc. React HooksΛ҆શʹ͏ React.js meetup #7 25.Jan.2019 -
Naoki Kobayashi
©2018 Wantedly, Inc. Naoki Kobayashi GitHub: @kobayang Twitter: @kbys_02 I’m
an Engineer @Wantedly
©2018 Wantedly, Inc. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ
©2018 Wantedly, Inc. Agenda - Hooks ΛదʹϝϞԽ͢Δ - react-hooks/exhaustive-deps Λઃఆ͢Δ
- stop-runaway-react-effects ͰϧʔϓΛࢹ͢Δ
©2018 Wantedly, Inc. HooksΛదʹϝϞԽ͢Δ
©2018 Wantedly, Inc. e.g. useRect: divͷେ͖͞Λऔಘ͢ΔCustom Hooks export const useRect
= () => { const [rect, setRect] = useState<ClientRect | DOMRect>(); const ref = useRef<HTMLDivElement | null>(null); const resize = () => { const target = ref.current; if (target) { const rect = target.getBoundingClientRect(); setRect(rect); } }; useEffect(() => resize(), [resize]); return { ref, rect }; }
©2018 Wantedly, Inc. e.g. useRect: divͷେ͖͞Λऔಘ͢ΔCustom Hooks export const useRect
= () => { const [rect, setRect] = useState<ClientRect | DOMRect>(); const ref = useRef<HTMLDivElement | null>(null); const resize = () => { const target = ref.current; if (target) { const rect = target.getBoundingClientRect(); setRect(rect); } }; useEffect(() => resize(), [resize]); return { ref, rect }; } resizeؔඳը͝ͱʹੜ͞Εͯ͠·͏
©2018 Wantedly, Inc. e.g. useRect: divͷେ͖͞Λऔಘ͢ΔCustom Hooks export const useRect
= () => { const [rect, setRect] = useState<ClientRect | DOMRect>(); const ref = useRef<HTMLDivElement | null>(null); const resize = useCallback(() => { const target = ref.current; if (target) { const rect = target.getBoundingClientRect(); setRect(rect); } }, []); useEffect(() => resize(), [resize]); return { ref, rect }; } useCallbackͰؔΛϝϞԽ͢Δ
©2018 Wantedly, Inc. e.g. counter const App: React.FC = ()
=> { const [count, setCount] = useState(0); const addCount = useCallback(() => { setCount(count + 1); }, []); return ( <div> <button onClick={() => addCount()}>Add Count</button> <p>count: {count}</p> </div> ); };
©2018 Wantedly, Inc. e.g. counter const App: React.FC = ()
=> { const [count, setCount] = useState(0); const addCount = useCallback(() => { setCount(count + 1); }, []); return ( <div> <button onClick={() => addCount()}>Add Count</button> <p>count: {count}</p> </div> ); }; count ͷࢀর͕ݹ͍
©2018 Wantedly, Inc. e.g. counter const App: React.FC = ()
=> { const [count, setCount] = useState(0); const addCount = useCallback(() => { setCount(count + 1); }, [count]); return ( <div> <button onClick={() => addCount()}>Add Count</button> <p>count: {count}</p> </div> ); }; count ΛୈೋҾʹՃ͢Δ
©2018 Wantedly, Inc. e.g. Hooks ΛదʹϝϞԽ͢Δ - useCallbackΛ༻͠ඳը͝ͱͷؔͷੜΛ͙ - useCallbackͷୈೋҾΛదʹઃఆ͢Δ
- useMemo Ͱಉ༷ Ͳ͏ͬͯ֬ೝ͢Δʁ
©2018 Wantedly, Inc. react-hooks/exhaustive-deps
©2018 Wantedly, Inc. react-hooks/exhaustive-deps ઃఆͯ͠·͔͢ʁ - eslintͷreact-hooksͷrule - ඳը͝ͱʹ͕ؔมߋ͞ΕΔ߹ -
HooksͰࢀর͞ΕΔมΛୈೋҾʹઃఆ͍ͯ͠ͳ͍߹
©2018 Wantedly, Inc. react-hooks/exhaustive-deps ઃఆͯ͠·͔͢ʁ ReactͷެࣜυΩϡϝϯτͰઃఆ͢Δ͜ͱΛ͓קΊ͍ͯ͠Δ IUUQTSFBDUKTPSHEPDTIPPLTSFGFSFODFIUNMVTFF⒎FDU
©2018 Wantedly, Inc. disableʹ͢Δ߹ඞͣཧ༝Λॻ͘ useEffect(() => { // apolloClient ඳը͝ͱʹੜ͞ΕͯΔͨΊઃఆ͠ͳ͍
// eslint-disable-next-line react-hooks/exhaustive-deps }, [paramId]); useEffect(() => { // mount࣌ʹҰ͚࣮ͩߦ͍ͨ͠ͷͰ deps Λۭʹ͍ͯ͠·͢ɻ // eslint-disable-next-line react-hooks/exhaustive-deps }, []);
©2018 Wantedly, Inc. stop-runaway-react-effects
©2018 Wantedly, Inc. exhaustive-deps ͰνΣοΫͰ͖ͳ͍έʔε const handleResize = debounce(resize, 16);
useEffect(() => { handleResize(); window.addEventListener("resize", handleResize); return () => { window.removeEventListener("resize", handleResize); }; }, [handleResize]); →ؒతʹ͕ؔมԽ͢Δ߹
©2018 Wantedly, Inc. stop-runaway-react-effects
©2018 Wantedly, Inc. stop-runaway-react-effects - Ͳͷ effect callback Ͱ ϧʔϓ͕ى͖͍ͯΔ͔
- ͲͷҾ͕มԽ͍ͯ͠Δ͔
©2018 Wantedly, Inc. ·ͱΊ - దʹϝϞԽΛߦ͍ແݶϧʔϓΛආ͚Δ - react-hooks/exhaustive-deps Λઃఆ͢Δ -
stop-runaway-react-effects ͰϧʔϓΛࢹ͢Δ