← Back to React Course | Chapter 9: State Management | Lesson 7 of 10

Zustand से State Management

Zustand fridge पर चिपका एक sticky note जैसा है जिसे घर में कोई भी directly पढ़ और update कर सकता है, बिना एक formal meeting की ज़रूरत के (Redux के ज़्यादा structured process के विपरीत)।
Syntax
markup
const useStore = create(set => ({
  value: initial,
  actionName: () => set(state => ({ value: state.value + 1 })),
}));

const value = useStore(state => state.value);
const actionName = useStore(state => state.actionName);

एक Zustand Store बनाना

Zustand का create() function एक store एक step में define करता है: आप एक function देते हैं जो set receive करता है (state update करने के लिए) और initial state plus कोई भी update functions return करता है, सब एक plain object में — अलग actions, reducers, या types की ज़रूरत नहीं।

Note: Redux के विपरीत, कोई Provider ज़रूरी नहीं — create() से बनाया store बस एक React hook है जो आप कहीं भी import और call कर सकते हैं।
Warning: Store को module scope पर एक बार define करें (किसी component के बाहर) — इसे एक component के अंदर बनाने से यह हर render पर reset हो जाएगा।

उदाहरण: Creating a Zustand Store

markup
// Run in your local React project (npm install required)
import { create } from 'zustand';

const useCounterStore = create(set => ({
  count: 0,
  increment: () => set(state => ({ count: state.count + 1 })),
}));

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Component में Store उपयोग करना

एक Zustand store बिल्कुल किसी और custom hook जैसे उपयोग होता है। useCounterStore(state => state.count) call करना component को सिर्फ उस field में subscribe करता है, सिर्फ तब re-render करते हुए जब वह बदलती है — कुछ Redux के useSelector जैसी spirit में, लेकिन store hook में ही directly built।

Note: सिर्फ वे specific fields/functions select करें जो आपको चाहिए, पूरे store object को नहीं, unnecessary re-renders से बचने के लिए।
Warning: बिना किसी selector के useCounterStore() call करना पूरा store state return करता है, component को किसी भी field में किसी भी change पर re-render कराते हुए।

उदाहरण: Using the Store in a Component

markup
// Run in your local React project (npm install required)
function Counter() {
  const count = useCounterStore(state => state.count);
  const increment = useCounterStore(state => state.increment);
  return <button onClick={increment}>Count: {count}</button>;
}

Zustand vs. Redux

Zustand Redux के कुछ strict structure (separate actions/reducers/types) को एक बहुत छोटे, ज़्यादा direct API से trade करता है, जो small-to-medium apps के लिए इसे शुरू करना तेज़ बनाता है।

Redux के stricter conventions बहुत बड़ी teams/codebases में ज़्यादा फायदा पहुंचा सकते हैं जहां वह structure consistency में मदद करता है।

Note: अगर एक team Redux के patterns और DevTools workflow से used to है, तो वह familiarity Zustand के छोटे learning curve के against तौलने लायक है।
Warning: Zustand की flexibility का मतलब है कम enforced structure — बड़े Zustand codebases को stores organize करने के लिए अपने own conventions set करने से फायदा होता है।

उदाहरण: Zustand vs. Redux

markup
// Run in your local React project (npm install required)
// Redux: separate action, reducer, dispatch, Provider, useSelector/useDispatch
// Zustand: one create() call, no Provider, direct hook usage:
const useStore = create(set => ({
  user: null,
  login: name => set({ user: name }),
}));
function Profile() {
  const user = useStore(s => s.user);
  return <p>{user || "Not logged in"}</p>;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. सिर्फ ज़रूरी specific field के बजाय एक component में पूरा store object select करना, extra re-renders का कारण बनते हुए।
  2. Store के set function के बाहर state को directly mutate करना।
  3. Module scope पर एक बार define करने के बजाय एक component के अंदर एक नया store instance बनाना।
चैप्टर सारांश
  • Zustand एक minimal state-management library है जिसमें Redux से कहीं कम boilerplate है।
  • एक store create() function से बनाया जाता है, state और update functions को साथ define करते हुए।
  • Components store hook को एक selector function के साथ call करके store से पढ़ते हैं।
  • कोई Provider component ज़रूरी नहीं है — store बस एक hook है जो आप directly import और उपयोग करते हैं।
ब्राउज़र सपोर्ट

npm install zustand चाहिए — इस sandbox में CDN से उपलब्ध नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.