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

useSelector और useDispatch का उपयोग

useSelector और useDispatch एक window (shared whiteboard में देखने के लिए) और एक pen (एक नई note भेजने के लिए) जैसे हैं जो कोई भी component Redux store से बात करने के लिए उपयोग कर सकता है।
Syntax
markup
const value = useSelector(state => state.sliceName.value);
const dispatch = useDispatch();

dispatch({ type: "actionType", payload: data });

useSelector से State पढ़ना

useSelector एक function लेता है जो पूरे Redux state को receive करता है और सिर्फ वह piece return करता है जो एक component को चाहिए। React-redux component को उस specific slice में subscribe करता है, इसलिए यह सिर्फ तब re-render होता है जब selected value असल में बदलती है, हर unrelated store update पर नहीं।

Note: Selector functions को छोटा और specific रखें, जैसे state => state.cart.items.length, बड़े objects को पूरी तरह select करने के बजाय।
Warning: पूरे state को select करना (state => state) optimization को defeat कर देता है — component literally हर store update पर re-render होता है।

उदाहरण: Reading State with useSelector

markup
// Run in your local React project (npm install required)
import { useSelector } from 'react-redux';

function ItemCount() {
  const count = useSelector(state => state.cart.items.length);
  return <p>Items: {count}</p>;
}

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

useDispatch से Actions Dispatch करना

useDispatch() store का dispatch function directly return करता है, जो एक component को event handlers के अंदर dispatch(action) call करके state changes trigger करने देता है, बिल्कुल store.dispatch() manually call करने जैसा, लेकिन under the hood Context के through store से wired।

Note: Returned dispatch function को एक variable में store करें (const dispatch = useDispatch()) एक बार, component के top के पास।
Warning: useDispatch() call करना सिर्फ आपको function देता है — change actually trigger करने के लिए आपको अब भी dispatch(someAction) call करना होगा।

उदाहरण: Dispatching Actions with useDispatch

markup
// Run in your local React project (npm install required)
import { useDispatch } from 'react-redux';

function AddButton() {
  const dispatch = useDispatch();
  return <button onClick={() => dispatch({ type: 'cart/addItem', payload: { id: 3 } })}>Add</button>;
}

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

दोनों को एक Component में Combine करना

ज़्यादातर components जो Redux के साथ interact करते हैं दोनों hooks साथ उपयोग करते हैं: current state दिखाने के लिए useSelector, और user actions के response में updates trigger करने के लिए useDispatch, store से एक complete read-and-write connection बनाते हुए।

Note: अपने Redux-connected components में एक clear, consistent structure के लिए दोनों hooks को component के top के पास destructure करें।
Warning: हर render पर useSelector के अंदर complex computed values inline derive करने से बचें — उन्हें component body में compute करें, selected raw data से।

उदाहरण: Combining Both in One Component

markup
// Run in your local React project (npm install required)
import { useSelector, useDispatch } from 'react-redux';

function Cart() {
  const items = useSelector(state => state.cart.items);
  const dispatch = useDispatch();
  return (
    <div>
      <p>{items.length} items</p>
      <button onClick={() => dispatch({ type: 'cart/clear' })}>Clear Cart</button>
    </div>
  );
}

⚠️ 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. सिर्फ ज़रूरी slice के बजाय पूरे state object को useSelector(state => state) से select करना, जो हर single change पर re-renders का कारण बनता है।
  2. useDispatch() call करना और actually returned function invoke करना भूल जाना।
  3. useSelector के अंदर inline एक नया object/array बनाना, unnecessary re-renders का कारण बनते हुए क्योंकि एक fresh reference last वाले जैसी कभी equal नहीं लगती।
चैप्टर सारांश
  • useSelector(fn) एक component को Redux store के एक specific slice में subscribe करता है, उस slice के बदलने पर re-render करते हुए।
  • useDispatch() actions trigger करने के लिए store का dispatch function return करता है।
  • सिर्फ वह specific data select करना जो एक component को चाहिए (पूरा state नहीं) unnecessary re-renders को minimize करता है।
  • दोनों hooks react-redux से आते हैं और component को एक <Provider> के अंदर होना ज़रूरी है।
ब्राउज़र सपोर्ट

npm install react-redux (hooks API react-redux 7.1+ में जोड़ा गया) चाहिए — इस 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.