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
wejs24.pdf
Search
Kodai Nakamura
September 25, 2018
1.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
wejs24.pdf
Kodai Nakamura
September 25, 2018
More Decks by Kodai Nakamura
See All by Kodai Nakamura
React Fire
kdnk
2
1.5k
Local State Management with Apollo
kdnk
2
1.6k
React Suspense on Apollo
kdnk
4
1.6k
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
400
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Optimizing for Happiness
mojombo
378
71k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.6k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Building the Perfect Custom Keyboard
takai
2
800
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Practical Orchestrator
shlominoach
191
11k
Transcript
©2018 Wantedly, Inc. setState Deep Dive Wejs@24 21.Sept.2018 - Kodai
Nakamura
©2018 Wantedly, Inc. setState Deep Dive Wejs@24 21.Sept.2018 - Kodai
Nakamura
©2018 Wantedly, Inc. Kodai Nakamura Github: @kdnk Twitter: @kdnk__ Software
Engineer at Wantedly, inc. JavaScript, TypeScript, React, Rails, Ruby
©2018 Wantedly, Inc. setState
©2018 Wantedly, Inc. increaseByTwo() { this.setState({ value: this.state.value + 1
}); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); }
©2018 Wantedly, Inc.
©2018 Wantedly, Inc.
©2018 Wantedly, Inc. ‣ ίϯϙʔωϯτ͝ͱʹ࣋ͯΔঢ়ଶͷ͜ͱ ‣ state ͕มΘΔͱ࠶ඳը͞ΕΔ ‣ setState
͕͋ͨΒ͍͠ state Λฦ͢ state
©2018 Wantedly, Inc. ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣
stateͷߋ৽batch setStateͷ͍ํ this.setState({comment: 'Hello'}); this.state.comment = 'Hello';
©2018 Wantedly, Inc. ҾʹΦϒδΣΫτΛ͢ increaseByTwo() { this.setState({ value: this.state.value +
1 }); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣ stateͷߋ৽batch
©2018 Wantedly, Inc. ͕͜͜batch increaseByTwo() { this.setState({ value: this.state.value +
1 }); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } ҾʹΦϒδΣΫτΛ͢ ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣ stateͷߋ৽batch
©2018 Wantedly, Inc. { ...this.state.value, value: this.state.value + 1, value:
this.state.value + 1, } ͕͜͜batch increaseByTwo() { this.setState({ value: this.state.value + 1 }); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } தͰ͜Μͳॲཧ͕ߦΘΕ͍ͯΔͣ ҾʹΦϒδΣΫτΛ͢ ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣ stateͷߋ৽batch
©2018 Wantedly, Inc. ‣ setStateͷҾʹΦϒδΣΫτ͚ͩͰͳ͘ɺؔΛ͢͜ͱͰ͖Δ ‣ ؔͷ࣮ߦඇಉظ ҾʹؔΛ͢ increaseByTwoCb() {
this.setState(prevState => ({ value: prevState.value + 1 })); this.setState(prevState => ({ value: prevState.value + 1 })); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); }
©2018 Wantedly, Inc. ‣ setStateͷҾʹΦϒδΣΫτ͚ͩͰͳ͘ɺؔΛ͢͜ͱͰ͖Δ ‣ ؔͷ࣮ߦඇಉظ increaseByTwoCb() { this.setState(prevState
=> ({ value: prevState.value + 1 })); this.setState(prevState => ({ value: prevState.value + 1 })); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } - ͕ؔΩϡʔʹՃ͞ΕΔ - prevState࠷৽Ͱ͋Δ͜ͱ͕ อূ͞ΕΔ ҾʹؔΛ͢
©2018 Wantedly, Inc. ‣ setState ͕ batching ͞ΕΔͷΠϕϯτϋϯυϥͷத͚ͩ ‣ ΠϕϯτϋϯυϥҎ֎ͩͱ
setState ͙͢ʹ࣮ߦ͞Εͯɺ͕ state ʹө͞ΕΔ ‣ ReactDOM.unstable_batchedUpdates Λ͏ͱΉΓΓ batching Ͱ͖Δ ‣ React 17(?) ͔Β ͯ͢ͷ setState ͕σϑΥϧτͰ batching ಉظతͳsetState
©2018 Wantedly, Inc. ‣ setState batch & ඇಉظ ‣
ͨͩ͠ɺΠϕϯτϋϯυϥҎ֎ͩͱɺಉظత ‣ React v17͔Βɺͯ͢ͷ setState ͕ඇಉظʹͳΔ ·ͱΊ