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
SOLIDから考える良いコンポーネントの設計原則
Search
Tetsuya Negishi
October 29, 2021
Technology
1.6k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SOLIDから考える良いコンポーネントの設計原則
Tetsuya Negishi
October 29, 2021
More Decks by Tetsuya Negishi
See All by Tetsuya Negishi
React Hooksで美しいコードを書く / Writing beautiful code using React Hooks
tetsuyanegishi
0
210
TypeScriptの型定義が JavaScriptのバグを駆逐する
tetsuyanegishi
3
970
React初学者が知らない コンポーネント分割テクニック
tetsuyanegishi
1
520
AWS Amplifyで作るサーバーレスバックエンド
tetsuyanegishi
3
2.1k
リアルタイムデータベース Cloud Firestore入門
tetsuyanegishi
10
9k
ブロックチェーンのマイニングとはなにか
tetsuyanegishi
1
110
スクラム開発の手法と実例
tetsuyanegishi
0
270
Other Decks in Technology
See All in Technology
IHV like なユースケースへのOpenID Connect 関連仕様の適用事例
optim
0
330
AWSとGitHub Actionsの責任境界と 組織で安全に使用する取り組み
nealle
0
200
:syncing_time:
sksat
3
810
アクセスキーが漏れた日にやるべきこと- 無効化の先にある本当の対応
kazzpapa3
0
350
フィジカルAIの知識ゼロの僕でも AIを使えばここまでできた
tatsuya1970
0
190
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
77k
AI時代のアウトプット――変わったこと、変わらないこと / Devsumi 2026 Kansai #devsumi
jnchito
0
600
Cloudflare製品を活用した AIガバナンス実践入門 / AI governance with Cloudflare Service
delta_tech
1
110
ClaudeCodeでセキュリティ監視業務を半自動化_1年の運用でわかったAIに任せる設計の5つのポイント
kintotechdev
3
770
Point Cloud as a Foreign Language for Multi-modal Large Language Model
takmin
0
170
顧客の要望は2次情報である 〜アンテナを張るFDEの構造論〜
noriakioji
3
970
GopherCon @シアトル に行ってきました
logica0419
0
320
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
390
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Building an army of robots
kneath
306
46k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Code Reviewing Like a Champion
maltzj
528
40k
Color Theory Basics | Prateek | Gurzu
gurzu
0
440
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
Transcript
© commmune Inc. All rights reserved SOLIDから考える 良いコンポーネントの設計原則 1 #ReactLT
© commmune Inc. All rights reserved 2 自己紹介 • 根岸徹也
• コミューン株式会社 副業:2018/11 ~ 2020/11 正社員:2020/12 ~ • フロントエンド + サーバサイド #ReactLT
© commmune Inc. All rights reserved 3 SOLIDとは #ReactLT
© commmune Inc. All rights reserved 4 SOLIDとは • 変更に強い・理解しやすい・
再利用しやすいコードを作るための設計原則 • オブジェクト指向を想定して提唱されたもの ◦ 考え方のコアの部分は普遍的なもの ◦ Reactのコンポーネントを作るときにも適用できる #ReactLT
© commmune Inc. All rights reserved 5 5つの原則 S:単一責任の原則 (SRP)
O:オープン・クローズドの原則 (OCP) L:リスコフの置換原則 (LSP) I:インターフェイス分離の原則 (ISP) D:依存関係逆転の原則 (DIP) #ReactLT
© commmune Inc. All rights reserved 6 5つの原則 S:単一責任の原則 (SRP)
O:オープン・クローズドの原則 (OCP) L:リスコフの置換原則 (LSP) I:インターフェイス分離の原則 (ISP) D:依存関係逆転の原則 (DIP) #ReactLT
© commmune Inc. All rights reserved 7 単一責任の原則 #ReactLT
© commmune Inc. All rights reserved 8 単一責任の原則 「モジュールを変更する理由はたった1つであるべきである」 #ReactLT
© commmune Inc. All rights reserved 9 単一責任の原則 「モジュールを変更する理由はたった1つであるべきである」 =>
役割を意識してコンポーネントを分ける とコンポーネントの変更理由が1つになりやすい #ReactLT
© commmune Inc. All rights reserved 10 単一責任の原則(ケーススタディ) #ReactLT
© commmune Inc. All rights reserved 11 単一責任の原則(ケーススタディ) • 円形画像のアイコン
-> <CircleImage /> • ユーザ名がボールド体 -> <Text bold /> • ユーザIDがグレーの文字 -> <Text color=“gray” /> • 左に画像アイコン, 右にユーザ情報 を表示 -> <MediaObjectLayout /> #ReactLT
© commmune Inc. All rights reserved 12 単一責任の原則(ケーススタディ) #ReactLT
© commmune Inc. All rights reserved 13 オープン・クローズドの 原則 #ReactLT
© commmune Inc. All rights reserved 14 オープン・クローズドの原則 「既存のコードを変更せず、新たにコードを追加するだけで振る舞い を変更できるようにするべき」
#ReactLT
© commmune Inc. All rights reserved 15 オープン・クローズドの原則 「既存のコードを変更せず、新たにコードを追加するだけで振る舞い を変更できるようにするべき」
=> propsで受け取る値の型をReact.ReactNode にすると既存コードを変更せずに振る舞いを 変更しやすい #ReactLT
© commmune Inc. All rights reserved 16 オープン・クローズドの原則(ケーススタディ) モーダルコンポーネントのpropsを考える #ReactLT
© commmune Inc. All rights reserved 17 オープン・クローズドの原則(ケーススタディ) #ReactLT
© commmune Inc. All rights reserved 18 オープン・クローズドの原則(ケーススタディ) タイトルの先頭にアイコンを入れたい 右下にボタンを入
れたい #ReactLT
© commmune Inc. All rights reserved 19 オープン・クローズドの原則(ケーススタディ) タイトルの先頭にアイコンを入れたい 右下にボタンを入
れたい React.ReactNodeにすると string, numberに加えて `<div>エレメント</div>` を受け取れるようになる #ReactLT
© commmune Inc. All rights reserved 20 オープン・クローズドの原則(ケーススタディ) propsの型を `React.ReactNode`
にするだけで拡張性が 段違いに上がる #ReactLT
© commmune Inc. All rights reserved 21 まとめ • SOLIDとは変更に強く、理解しやすく、再利用しやすいコードを
作るための設計原則 • 単一責任の原則を守るために 役割を意識してコンポーネントを分ける • オープン・クローズドの原則を守るために React.ReactNode型をpropsに活用すべき #ReactLT
© commmune Inc. All rights reserved 22 参考 • Atomic
Design ~堅牢で使いやすいUIを効率良く設計する [技術評論社 (2018/4/25)] ◦ コンポーネント分割のテクニックを学びたい人にオススメ • Clean Architecture 達人に学ぶソフトウェアの構造と設計 [ドワンゴ (2018/8/1)] ◦ SOLIDについて知りたい人向け • 良いコードとは何か - エンジニア新卒研修 スライド公開 (https://note.com/cyberz_cto/n/n26f535d6c575) ◦ SOLIDを守るとなぜ良いコードになるのか背景がよく分かる #ReactLT