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
Disciplined Vibes: Scaling AI-Assisted Engineering
sheharyar
0
110
AIっぽい文章を採点して人間らしく直すアプリを作ってみた
yama3133
2
110
タクシーアプリ『GO』の実践的データ活用
mot_techtalk
3
190
AI駆動開発が変える、大規模開発の前提 ーHuman in the Loop から Human on the Loop へ / AIE2026
visional_engineering_and_design
30
23k
白金鉱業Meetup_Vol.24_「AIエージェントは分けるほど良い」は本当か? / Is it true that “the more you divide AI agents, the better”?
brainpadpr
1
120
AmazonRoute 53ではじめてのドメイン取得!HTTPS化までの道のりを整理してみた
usanchuu
3
120
失敗を資産に変えるClaude Code
shinyasaita
0
200
Kubernetesにおける学習基盤とLLMOpsの概要
ry
1
210
"何を作るか"を任される エンジニアは、どう育つのか
yutaokafuji
1
510
DevOps Agentで始めるAWS運用 〜フロンティアエージェントが変える運用の現場〜
nyankotaro
1
360
2026.06.13_AI時代に事業会社が「SIer出身エンジニア」を求める理由 / Why Businesses Seek Engineers with a System Integrator Background in the AI Era
jumtech
0
1k
noUncheckedIndexedAccess、3時間、1万円。 / noUncheckedIndexedAccess, 3 Hours, 10,000 JPY.
kaonavi
1
340
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
141
7.5k
Marketing to machines
jonoalderson
1
5.4k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
A designer walks into a library…
pauljervisheath
211
24k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
300
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
360
Design in an AI World
tapps
1
230
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
360
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
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 ͰϧʔϓΛࢹ͢Δ