Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Zustandを用いた実践的状態管理
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Nokogiri
July 17, 2025
950
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Zustandを用いた実践的状態管理
Nokogiri
July 17, 2025
More Decks by Nokogiri
See All by Nokogiri
React Testing Libraryでの WAI-ARIAロールの活用事例
undefined_name
1
230
自動テストは何の役に立つのか そして役に立たないのか
undefined_name
5
1.9k
Pipe Operator (|>) の紹介
undefined_name
2
420
FizzBuzzで学ぶOCP
undefined_name
0
180
エンジニアとQAでコラボするフロントエンドリアーキテクチャ開発の事例
undefined_name
4
3.3k
オブジェクト指向のプラクティスをフロントエンドで活用する
undefined_name
7
1.8k
モププロ@kintone開発チーム
undefined_name
1
680
勉強会で登壇者に 質問しづらい課題を解決する サービスをリリースしました🎉
undefined_name
2
1.4k
Usefull GitLens
undefined_name
3
930
Featured
See All Featured
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.8k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.9k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
60k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1.1k
The World Runs on Bad Software
bkeepers
PRO
72
12k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
450
How GitHub (no longer) Works
holman
316
150k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.3k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
560
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
550
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
A Soul's Torment
seathinner
8
3.9k
Transcript
1
自己紹介 株式会社カケハシ 生成AI研究開発チーム ソフトウェアエンジニア Nokogiri(@nkgrnkgr) 2
株式会社カケハシ 医療体験をしなやかに 主に薬局向けの業務シ ステムを提供 ヘルステックスタート アップ 3
今日話すこと 1. useState以上の状態管理が必要なケース 2. Zustandを使った設計・実装プラクティス 3. まとめ 話さないこと 1. Zustand
の基本的な使い方 2. 他のライブラリとの比較 4
useState以上の状態管理が必要なケース 状態間の依存 散在する状態更新ロジック 頻繁な再描画によるパフォーマンス問題 5
具体的な事例:AI在庫の入庫ダイアログ 動的に変わる初期値 更新時に他の状態を更新する 6
7
Zustandによる解決 中央集権的な状態管理 アクションによる状態更新の集約 セレクターによる効率的な再描画制御 8
Zustand 具体的なプラクティス 9
1. Actions に状態更新ロジックを集約する const useStoreStockDialogStore = create<StoreStockDialogStore>()((set) => ({ actions:
{ updateStoreStockReason: (reason) => { set((state) => ({ ...state, // プライマリ状態の更新 storeStockInfoFormValues: { ...state.storeStockInfoFormValues, storeStockReason: reason, // 他の状態への副作用 counterPartyId: INITIAL_FORM_VALUES.counterPartyId, counterPartyName: INITIAL_FORM_VALUES.counterPartyName, }, stockOperationConfig: { ...state.stockOperationConfig, counterParty: null, }, })); }, }, })); 10
2. セレクターによる効率的な状態参照 // selectors.ts export const selectFeeAmount = (state: StoreStockDialogStore)
=> state.storeStockInfoFormValues.feeAmount; // Component const feeAmount = useStoreStockDialogStore(selectFeeAmount); 状態の参照を一箇所に集約 Stateのデータ構造を変えてもコンポーネント側に影響を出さない 純粋関数なのでテストしやすい 11
3. useShallow を使った配列への浅い参照 const selectMedicineIds = (state: StoreStockDialogStore) => state.medicineInfoList.map((i)
=> i.medicineId); function MedicineList() { const ids = useStoreStockDialogStore(useShallow(selectMedicineIds)); return ( <ul> {ids.map((id) => ( <MedicineListItem key={id} medicineId={id} /> ))} </ul> ); } 配列の内容が変わらなければ再描画を防ぐ。パフォーマンスの向上 createSelector でも代替可能 12
4. 末端コンポーネントでの状態参照 function MedicinePrice() { const medicinePrice = useStoreStockDialogStore(selectMedicinePrice); return
( <input type="text" value={medicine.price} /> ) } 必要な状態のみを参照 不要な再描画を防ぐ 13
5. Store のライフサイクル管理 // StoreProvider.tsx export const StoreProvider = ({
children }: { children: ReactNode }) => { const storeRef = useRef<StoreApi<StoreStockDialogStore>>(); if (!storeRef.current) { storeRef.current = createStoreStockDialogStore(); } return ( <StoreContext.Provider value={storeRef.current}> {children} </StoreContext.Provider> ); }; Initialize state with props React コンポーネントのライフサイクルと連動 Stateの破棄漏れを防ぐ 14
6. immer を利用した安全な更新 const useStore = create<Store>()( immer((set) => ({
nested: { value: 0 }, actions: { updateNested: (value) => { set((state) => { state.nested.value = value; // 直接代入可能 }); }, }, })) ); 不変性を保ちながら直感的な更新 バグの減少 15
まとめ 開発体験の向上 コードの可読性と保守性の向上 アクションによる明確な状態更新 セレクターによる統一的な状態参照 パフォーマンス向上 再レンダリング回数の大幅削減 複雑な状態管理でもスムーズな動作 詳細記事: https://kakehashi-dev.hatenablog.com/entry/2024/09/10/110000
16
17
18
We are Hiring! 株式会社カケハシで一緒に働きま せんか? ソフトウェアエンジニア募集中! 医療体験をしなやかにする技術開 発 採用情報: https://kakehashi-
dev.com/recruit/ 19
20