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

Atomic State: Recoil और Jotai

Recoil और Jotai जैसी atomic state libraries हर single fact को अपना sticky note देने जैसी हैं एक giant shared whiteboard के बजाय, ताकि एक fact बदलने से किसी और चीज़ के बारे में notes disturb न हों।
Syntax
markup
// Recoil
const nameAtom = atom({ key: "uniqueKey", default: initial });
const [value, setValue] = useRecoilState(nameAtom);

// Jotai
const nameAtom = atom(initial);
const [value, setValue] = useAtom(nameAtom);

'Atomic' State का मतलब क्या है

एक बड़े state object के बजाय (जैसे Redux में या एक single Context में), atomic libraries state को कई छोटे, independent pieces में split करती हैं जिन्हें atoms कहा जाता है।

हर atom खुद का एक unit of state है जिसे components individually पढ़ और update कर सकते हैं, unrelated data को touch किए बिना।

Note: एक atom को किसी एक component के बाहर रहने वाले एक single useState की तरह सोचें, जो कोई भी component इसे import करके share कर सके।
Warning: tightly-related data के लिए बहुत ज़्यादा tiny atoms में split करना उन्हें sync में रखना उससे ज़्यादा काम बना सकता है जितने लायक है — जब sense बने तो closely related fields को group करें।

उदाहरण: What 'Atomic' State Means

markup
// Run in your local React project (npm install required)
// Recoil
import { atom, useRecoilState } from 'recoil';

const countAtom = atom({ key: 'count', default: 0 });
function Counter() {
  const [count, setCount] = useRecoilState(countAtom);
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}

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

Recoil की RecoilRoot Requirement

Recoil को app को एक single <RecoilRoot> component में wrap करना चाहिए, Redux के Provider या React Router के BrowserRouter जैसा, ताकि इसका internal atom-tracking system काम कर सके। इसके बिना, useRecoilState और related hooks काम नहीं करेंगे।

Note: <RecoilRoot> को एक बार, अपने app के top के पास रखें, बिल्कुल दूसरे provider-style wrappers जैसे।
Warning: RecoilRoot भूल जाना एक runtime error produce करता है जिस moment कोई भी component Recoil hook उपयोग करने की कोशिश करे।

उदाहरण: Recoil's RecoilRoot Requirement

markup
// Run in your local React project (npm install required)
import { RecoilRoot } from 'recoil';
import App from './App';

ReactDOM.createRoot(document.getElementById('root')).render(
  <RecoilRoot>
    <App />
  </RecoilRoot>
);

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

Jotai: बिना Provider के Atomic State

Jotai Recoil जैसी एक similar atomic philosophy follow करता है लेकिन बिल्कुल बिना किसी wrapping Provider के काम करने के लिए designed है — atom() से defined atoms useAtom hook के साथ tree में कहीं भी directly उपयोग किए जा सकते हैं, setup को और भी lighter बनाते हुए।

Note: Jotai का API (atom + useAtom) useState के shape को closely mirror करता है, जो plain React से सीधे आने वाले बहुतों को intuitive लगता है।
Warning: Jotai में Provider की कमी का मतलब है एक साथ सभी atoms reset करने (जैसे logout पर) के लिए कोई single obvious जगह नहीं है — ज़रूरत पड़ने पर इसे per-atom handle करें।

उदाहरण: Jotai: Atomic State Without a Provider

markup
// Run in your local React project (npm install required)
// Jotai — no Provider required
import { atom, useAtom } from 'jotai';

const countAtom = atom(0);
function Counter() {
  const [count, setCount] = useAtom(countAtom);
  return <button onClick={() => setCount(c => c + 1)}>{count}</button>;
}

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

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. Module scope के बजाय एक component के render के अंदर एक नई atom definition बनाना, जो इसे बार-बार reset करती है।
  2. यह भूल जाना कि Recoil को app को <RecoilRoot> में wrap करना चाहिए, Redux के Provider जैसा।
  3. Atoms को जैसे वे सिर्फ primitive values रख सकते हों treat करना — वे objects और arrays बिल्कुल ठीक से रख सकते हैं।
चैप्टर सारांश
  • Atomic state management state को एक बड़े store के बजाय छोटे, independent units में split करता है जिन्हें atoms कहा जाता है।
  • Recoil को एक <RecoilRoot> wrapper चाहिए; Jotai को किसी wrapper की ज़रूरत नहीं है।
  • Components individual atoms में subscribe करते हैं, इसलिए एक atom बदलने से सिर्फ वे components re-render होते हैं जो उस specific atom को उपयोग करते हैं।
  • दोनों libraries plain useState के करीब feel करने के लिए designed हैं, Redux के action/reducer model के बजाय।
ब्राउज़र सपोर्ट

npm install recoil या npm install jotai चाहिए — इस 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.