<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anurag Jain</title>
    <description>The latest articles on DEV Community by Anurag Jain (@buddylonglegs).</description>
    <link>https://dev.to/buddylonglegs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1430513%2F8d88e432-4874-40cd-b283-13f398f2fa60.jpeg</url>
      <title>DEV Community: Anurag Jain</title>
      <link>https://dev.to/buddylonglegs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/buddylonglegs"/>
    <language>en</language>
    <item>
      <title>Understanding Redux Internals: How It Works Under the Hood</title>
      <dc:creator>Anurag Jain</dc:creator>
      <pubDate>Sun, 16 Mar 2025 16:34:07 +0000</pubDate>
      <link>https://dev.to/buddylonglegs/understanding-redux-internals-how-it-works-under-the-hood-43dc</link>
      <guid>https://dev.to/buddylonglegs/understanding-redux-internals-how-it-works-under-the-hood-43dc</guid>
      <description>&lt;p&gt;Redux is a widely used state management library in the React ecosystem, known for its predictable state updates and unidirectional data flow. While most developers use Redux with actions, reducers, and the &lt;code&gt;useSelector&lt;/code&gt;/&lt;code&gt;useDispatch&lt;/code&gt; hooks, few dive deep into &lt;em&gt;how&lt;/em&gt; Redux actually works under the hood.&lt;/p&gt;

&lt;p&gt;In this blog, we’ll peel back the layers of Redux and explore its core principles, including how actions are processed, how reducers update the state, and how the store efficiently notifies subscribers. By the end, you’ll have a deeper understanding of Redux’s internals, allowing you to debug issues more effectively and even build a simplified version of Redux yourself!&lt;/p&gt;

&lt;p&gt;Let’s dive in. 🚀&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Breaking Down Redux: Core vs React Bindings&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To truly understand Redux under the hood, we need to separate its two key parts:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Redux Toolkit (RTK) – The Core of Redux&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Redux itself is a standalone state management library that works independently of React. Redux Toolkit (RTK) is the modern way to use Redux, providing a more convenient API while keeping the core principles intact. It consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Store&lt;/strong&gt; – The centralized state container&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reducers&lt;/strong&gt; – Pure functions that determine how the state changes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Actions&lt;/strong&gt; – Descriptive objects that trigger state updates&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Middleware&lt;/strong&gt; – Custom logic that intercepts dispatched actions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Immer &amp;amp; Redux-Thunk&lt;/strong&gt; – Built-in utilities for immutability and async logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. React-Redux – Connecting Redux to React&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While Redux is framework-agnostic, React-Redux is the official binding library that integrates Redux with React components. It provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Provider&lt;/code&gt;&lt;/strong&gt; – Makes the Redux store accessible to the React app&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;useSelector&lt;/code&gt;&lt;/strong&gt; – Fetches state from the store within a component&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;useDispatch&lt;/code&gt;&lt;/strong&gt; – Allows dispatching actions from components&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Context-based optimizations&lt;/strong&gt; – Ensures efficient re-renders in React&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By understanding these two parts separately, you’ll gain a clear picture of how Redux and React-Redux work together to manage state efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Redux Toolkit (RTK) – The Core of Redux&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At its heart, Redux operates on the &lt;strong&gt;publish-subscribe (pub-sub) pattern&lt;/strong&gt;. The core idea is simple:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;strong&gt;store&lt;/strong&gt; holds the global state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dispatching an action&lt;/strong&gt; tells Redux that something has changed.&lt;/li&gt;
&lt;li&gt;The store &lt;strong&gt;calls the appropriate reducer&lt;/strong&gt;, which updates the state immutably.&lt;/li&gt;
&lt;li&gt;Once the state is updated, &lt;strong&gt;all subscribed listeners are notified&lt;/strong&gt;, allowing them to take action based on the change.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This predictable data flow ensures that state updates remain &lt;strong&gt;centralized, immutable, and traceable&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Breaking Down the Core Redux Flow&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Store Creation&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Redux maintains a &lt;strong&gt;single store&lt;/strong&gt; that holds the application’s state.&lt;/li&gt;
&lt;li&gt;The store is created using &lt;code&gt;configureStore()&lt;/code&gt; in Redux Toolkit or &lt;code&gt;createStore()&lt;/code&gt; in vanilla Redux.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Dispatching an Action&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When an event occurs (like a button click), we dispatch an &lt;strong&gt;action&lt;/strong&gt; (a plain JavaScript object with a &lt;code&gt;type&lt;/code&gt; property).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;counter/increment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Reducers Process the Action&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The store passes the current state and the action to the &lt;strong&gt;reducer function&lt;/strong&gt;, which returns a new state.&lt;/li&gt;
&lt;li&gt;Redux Toolkit simplifies this using &lt;code&gt;createSlice()&lt;/code&gt;, where reducers are auto-generated.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Example reducer:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counterSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSlice&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;counter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;initialState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;State Updates &amp;amp; Listeners Get Notified&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After the state is updated, Redux &lt;strong&gt;notifies all subscribed components or middleware&lt;/strong&gt;, allowing them to react to the change.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Immutability Matters&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Redux ensures that &lt;strong&gt;state is never mutated directly&lt;/strong&gt;. Instead, reducers return &lt;strong&gt;a new copy&lt;/strong&gt; of the state. Redux Toolkit simplifies this by using &lt;strong&gt;Immer&lt;/strong&gt;, which allows us to write "mutating" code that is safely converted to immutable updates behind the scenes.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;React-Redux – Connecting Redux to React&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Redux is a framework-agnostic state management library, meaning it doesn't depend on React. However, in a React application, we need a way to &lt;strong&gt;connect&lt;/strong&gt; Redux to components efficiently. This is where &lt;strong&gt;React-Redux&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;At its core, React-Redux relies on &lt;strong&gt;React Context&lt;/strong&gt; and React’s built-in hooks to provide seamless state management. Let’s break it down:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How Redux Store is Provided to React Components&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The Redux store is stored in a &lt;strong&gt;React Context&lt;/strong&gt; that wraps the entire app.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Provider&lt;/code&gt; component from React-Redux is simply a wrapper around this &lt;strong&gt;Redux context provider&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;This allows any component within the app to access the Redux store without passing props manually.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Example: How &lt;code&gt;Provider&lt;/code&gt; Works&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./store&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Internally, &lt;code&gt;Provider&lt;/code&gt; stores the &lt;strong&gt;Redux store&lt;/strong&gt; inside a &lt;strong&gt;React Context&lt;/strong&gt; and makes it accessible to all components.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;How &lt;code&gt;useSelector&lt;/code&gt; Works Under the Hood&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;useSelector&lt;/code&gt; hook is responsible for &lt;strong&gt;selecting a piece of state&lt;/strong&gt; from the Redux store inside a component.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;How It Works:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;It gets the Redux store from context&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It subscribes to state updates using &lt;code&gt;useSyncExternalStore&lt;/code&gt;&lt;/strong&gt;, a built-in React hook for listening to external stores&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It extracts only the relevant part of the state&lt;/strong&gt; (to optimize performance)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Example: Using &lt;code&gt;useSelector&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useSelector&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useSelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Why &lt;code&gt;useSyncExternalStore&lt;/code&gt;?&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;React-Redux &lt;strong&gt;used to rely on &lt;code&gt;useEffect&lt;/code&gt; and &lt;code&gt;useState&lt;/code&gt; for subscriptions&lt;/strong&gt;, but this led to &lt;strong&gt;performance issues and stale state bugs&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useSyncExternalStore&lt;/code&gt; ensures that React always &lt;strong&gt;reads the latest state&lt;/strong&gt; and minimizes unnecessary re-renders.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;How &lt;code&gt;useDispatch&lt;/code&gt; Works Under the Hood&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;useDispatch&lt;/code&gt; hook allows components to &lt;strong&gt;dispatch actions&lt;/strong&gt; to the Redux store.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;How It Works:&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;It gets the Redux store from context&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It returns the store’s &lt;code&gt;dispatch&lt;/code&gt; function&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Example: Using &lt;code&gt;useDispatch&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useDispatch&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./counterSlice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;IncrementButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDispatch&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Increment&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike &lt;code&gt;useSelector&lt;/code&gt;, &lt;code&gt;useDispatch&lt;/code&gt; does not subscribe to state changes—it only provides the &lt;code&gt;dispatch&lt;/code&gt; function.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Optimisations in React-Redux&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Context is only used for accessing the store, not state updates&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Redux’s state changes &lt;strong&gt;do not trigger unnecessary re-renders&lt;/strong&gt; like regular React Context.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;useSelector&lt;/code&gt; prevents unnecessary re-renders&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Components only re-render when the &lt;strong&gt;selected state changes&lt;/strong&gt;, thanks to &lt;code&gt;useSyncExternalStore&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Wrapping Up: Understanding Redux Inside Out&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;By now, you should have a clear understanding of how Redux and React-Redux work under the hood. We explored:&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;Redux Toolkit (RTK)&lt;/strong&gt; – The core state management logic built on the &lt;strong&gt;pub-sub model&lt;/strong&gt;, where a store holds the state, reducers handle updates immutably, and subscribed listeners react to changes.&lt;/p&gt;

&lt;p&gt;✅ &lt;strong&gt;React-Redux&lt;/strong&gt; – The binding layer that efficiently connects Redux to React using &lt;strong&gt;React Context&lt;/strong&gt; and &lt;strong&gt;hooks like &lt;code&gt;useSelector&lt;/code&gt; and &lt;code&gt;useDispatch&lt;/code&gt;&lt;/strong&gt;, leveraging &lt;code&gt;useSyncExternalStore&lt;/code&gt; for optimal reactivity.&lt;/p&gt;

&lt;p&gt;Understanding these internals helps you:&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Debug complex state issues&lt;/strong&gt; with confidence&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Optimize performance&lt;/strong&gt; by avoiding unnecessary re-renders&lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Customize Redux behavior&lt;/strong&gt; or even build your own minimal Redux-like system&lt;/p&gt;

&lt;p&gt;At the end of the day, Redux is just &lt;strong&gt;a predictable state container with a well-defined flow&lt;/strong&gt;. While libraries like Redux Toolkit and React-Redux make it easier to use, the core principles remain simple: &lt;strong&gt;dispatch an action → reducer updates the state → components react accordingly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next time you're using Redux, take a moment to appreciate the elegant architecture behind it.&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
