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でDecoratorsを使う
Search
Masashi Hirano
March 07, 2018
Programming
0
1k
ReactでDecoratorsを使う
React Beer Bash! (
https://react-beer-bash.connpass.com/event/78112/
) でLTしました
Masashi Hirano
March 07, 2018
Tweet
Share
More Decks by Masashi Hirano
See All by Masashi Hirano
You may not need XXX in Node.js
masashi
5
1.3k
OSSとコミュニティを支える
masashi
1
1.1k
英語ができなかった自分達が、グローバルチーム立ち上げに挑戦!?
masashi
1
3.2k
フロントエンド開発のためのセキュリティ入門について
masashi
1
480
フロントエンド開発のためのセキュリティ入門
masashi
49
17k
Node.jsの2022年と未来 / Node.js in 2022 and Future
masashi
1
1.1k
Corepack ~Node.jsに追加されたパッケージマネージャーマネージャー~ / #tng37
masashi
3
12k
Node.js + Web Compatibility
masashi
2
620
サイボウズが行うフロントエンドの品質保証 / Frontend Quality Assurance at Cybozu
masashi
10
2.9k
Other Decks in Programming
See All in Programming
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
870
.NET Frameworkでも汎用ホストが使いたい!
tomokusaba
0
190
もう少しテストを書きたいんじゃ〜 #phpstudy
o0h
PRO
8
590
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
170
Datadog Workflow Automation で圧倒的価値提供
showwin
1
100
お前もAI鬼にならないか?👹Bolt & Cursor & Supabase & Vercelで人間をやめるぞ、ジョジョー!👺
taishiyade
7
4.1k
昭和の職場からアジャイルの世界へ
kumagoro95
1
420
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
230
Jakarta EE meets AI
ivargrimstad
0
260
CloudNativePGを布教したい
nnaka2992
0
110
Introduction to kotlinx.rpc
arawn
0
750
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
53
18k
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
10
510
Producing Creativity
orderedlist
PRO
344
40k
Side Projects
sachag
452
42k
Music & Morning Musume
bryan
46
6.4k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
A Tale of Four Properties
chriscoyier
158
23k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
4
360
Designing Experiences People Love
moore
140
23k
Into the Great Unknown - MozCon
thekraken
35
1.6k
Building an army of robots
kneath
303
45k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Transcript
ReactͰDecoratorsΛ͏ 2018/03/07 React Beer Bash ฏণ࢜ / @shisama
{ "about": { "name": "Masashi Hirano", "works": "Weblio, Inc.", "twitter":
"@shisama_", "github": "shisama" } }
͜ͷൃදʹ͍ͭͯ • JavaScriptͷ࠷ۙػೳΛઌऔΓͯ͠ReactͰ ͏ࣄྫΛհ • Reactͬͨ͜ͱ͋Δਓ͚ • ίʔυΛ؆ܿʹॻ͖͍ͨਓ͚
Agenda • Decoratorsͷհ • Decoratorsͷ࣮ྫ
Decoratorsͱ • EcmaScriptυϥϑτ(stage-2)ͷػೳ • ॲཧΛՃͨ͠Γஔ͖͑ͨΓͰ͖Δ • @ϚʔΫΛ͚ͨهड़ • JavaͷΞϊςʔγϣϯPythonͷσίϨʔ λʔΈ͍ͨͳهड़
• ໊લͷ༝དྷGoFͷσίϨʔλʔύλʔϯ
ྫ͑ϓϩύςΟͷՃ function name(name) { return function (target) { target.prototype.name =
name; } } ΫϥεʹOBNFͱ͍͏ϓϩύςΟΛՃ͢Δ ؔΛఆٛ
@name(“React") class App extends React.Component { ɹrender() { return (
<div> Hello, {this.name} </div> ); } } ReactDOM.render(<App />, document.getElementById('app')); ͜ͷίϯϙʔωϯτࣗମOBNFͱ͍ ͏ϓϩύςΟ͍࣋ͬͯͳ͍ σίϨʔλʔ
ϨϯμϦϯά݁Ռ Hello, React σίϨʔλʔؔʹΑͬͯOBNFͷϓ ϩύςΟ͕Ճ͞Εͨ
DecoratorsΛ͏ํ๏ • Babel • babel-plugin-transform-decorators-legacy Λ͏ɻ • TypeScript • tsc
--experimentalDecorators
Decoratorsͷࣄྫ
react-redux https://github.com/reactjs/react-redux
react-redux • ReduxͱstateΛཧ͢ΔϥΠϒϥϦ • react-reduxReactͱReduxΛͭͳ͛ΔϥΠϒ ϥϦ
react-redux class MyFancyComponent extends React.Component { // ׂѪ } const
mapStateToProps = (state) => { return { users: state.users }; } const mapDispatchToProps = (dispatch, ownProps) { return {// ׂѪ} } export default connect(mapStateToProps, mapDispatchToProps)(MyFancyComponent) SFBDUSFEVYͷDPOOFDUؔɻ Α͘ݟΔ͍ํ
react-redux @connect(mapStateToProps, mapDispatchToProps) class MyFancyComponent extends React.Component { // ׂѪ
} const mapStateToProps = (state) => { return { users: state.users }; } const mapDispatchToProps = (dispatch, ownProps) { return {// ׂѪ} } DPOOFDUΛσίϨʔλʔͱͯ͠$MBTT ʹ͚Δ͜ͱ͕Ͱ͖Δ
MobX https://mobx.js.org/best/decorators.html
MobX • ReduxΈ͍ͨʹstateΛཧ͢ΔϥΠϒϥϦ • ReduxΑΓ؆ܿʹॻ͚Δҹ
MobX class Timer { constructor() { extendObservable(this, { start: Date.now(),
current: Date.now(), get elapsedTime() { return (this.current - this.start) }, tick: action(function() { this.current = Date.now() }) }) } } FYUFOE0CTFSWBCMFؔΛͬͯ PCTFSWBCMFʹ͍ͯ͠Δ
MobX class Timer { @observable start = Date.now(); @observable current
= Date.now(); @computed get elapsedTime() { return (this.current - this.start) } @action tick() { this.current = Date.now() } } σίϨʔλʔΛͬͯ؆ܿʹॻ͚Δ
MobX https://mobx.js.org/best/decorators.html ެࣜʹࡌ͍ͬͯΔ
react-log-decorator https://github.com/shisama/react-log-decorator
react-log-decorator • ࡞ͬͨ • npm install —save react-log-decoratorͰೖΔ • rendercomponentDidMountͳͲͷϝιο
υ͕ݺΕͨͱ͖ʹpropsͱstateͱҾͷ Λίϯιʔϧʹදࣔ͢ΔσόοΨʔ
react-log-decorator import logger from 'react-log-decorator'; const log = logger(process.env.NODE_ENV ===
'development'); export default class MyComponent extends Component { @log render() { return ( <div> <input type="text" onChange = {this.props.onChange} /> <p>{this.props.message}</p> </div> ) } } ࣮ߦ࣌ʹQSPQTTUBUFͷ༰Λίϯ ιʔϧʹද͍ࣔͨ͠ϝιουʹ͚Δ
SFOEFS͕࣮ߦ͞ΕΔͨͼʹQSPQT TUBUFͷΛදࣔ react-log-decorator
pure-deep-equal https://github.com/shisama/pure-deep-equal
pure-deep-equal • ࡞ͬͨ • npm install —save pure-deep-equalͰೖΔ • shouldComponentUpdateͷதͰdeep
compare͢Δ
pure-deep-equal ࡞ͬͨܦҢ͜͜
pure-deep-equal class Test extends React.Component { shouldComponentUpdate(nextProps, nextState) { return
!deepEqual(this.props, nextProps) || !deepEqual(this.state, nextState); } render() { return <span>{this.props.message}</h1> } } TIPVME$PNQPOFOU6QEBUFͷதͰ EFFQDPNQBSF
pure-deep-equal @PureDeepEqual class Test extends React.Component { render() { return
<span>{this.props.message}</h1> } } TIPVME$PNQPOFOU6QEBUFΛ࣮͠ ͳͯ͘σίϨʔλ͕ؔEFFQ DPNQBSFͯ͘͠ΕΔ
Ͳ͏ͬͯ Decorators࡞Δͷʁ
https://qiita.com/shisama/items/ 45f07f39a46a9e7fa85a
࠷ޙʹ • DecoratorsΛ͑ίʔυ͕؆ܿʹͳΔ • ͚ͨΓ֎ͨ͠Γ͕؆୯ʹߦ͑ɺΞεϖΫτ ࢦతʹ͑Δ • ͨͩ͠ɺ·ͩఏҊதͷػೳͳͷͰ༷͕มΘ ΔՄೳੑ͋Δ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠