<?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: Thamizh Arasan</title>
    <description>The latest articles on DEV Community by Thamizh Arasan (@thamizh_arasan_6f93e2f9b5).</description>
    <link>https://dev.to/thamizh_arasan_6f93e2f9b5</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%2F2950536%2F7752b189-0572-40e6-b401-e10150aa26fc.jpg</url>
      <title>DEV Community: Thamizh Arasan</title>
      <link>https://dev.to/thamizh_arasan_6f93e2f9b5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thamizh_arasan_6f93e2f9b5"/>
    <language>en</language>
    <item>
      <title>Not Just Another Redux Article: Learning Redux from Its Source Code</title>
      <dc:creator>Thamizh Arasan</dc:creator>
      <pubDate>Sun, 04 May 2025 09:11:00 +0000</pubDate>
      <link>https://dev.to/thamizh_arasan_6f93e2f9b5/not-just-another-redux-article-learning-redux-from-its-source-code-1c2j</link>
      <guid>https://dev.to/thamizh_arasan_6f93e2f9b5/not-just-another-redux-article-learning-redux-from-its-source-code-1c2j</guid>
      <description>&lt;p&gt;Let’s go beyond the usual Redux tutorials. In this case study, we’ll learn the core concepts and APIs of Redux directly from its source code. I promise, this approach is both fun and much easier to understand!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do we always need Redux?&lt;/strong&gt; No.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Is Redux useful?&lt;/strong&gt; Absolutely-yes, yes, yes!&lt;/p&gt;

&lt;p&gt;Choosing Redux is a bit like choosing &lt;code&gt;useMemo()&lt;/code&gt; in React: it’s a powerful tool, but only when there’s a real need. There are alternative ways to solve problems that Redux addresses. For example, &lt;a href="https://react.dev/reference/react/useMemo#should-you-add-usememo-everywhere" rel="noopener noreferrer"&gt;code splitting&lt;/a&gt; can help us avoid using &lt;code&gt;useMemo()&lt;/code&gt; and encourage us to think in a more "React way." So, should we avoid Redux? Or are there specific conditions when Redux is the right choice? To answer this, let’s not just follow the advice of experts like Dan Abramov. Instead, let’s dig deeper: &lt;a href="https://redux.js.org/style-guide/" rel="noopener noreferrer"&gt;&lt;strong&gt;what are the reasons behind the rules and pitfalls these experts suggest?&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learning by Building:&lt;/strong&gt; One of the best ways to understand Redux is to build a simple version yourself. There are many tutorials online, like &lt;a href="https://gist.github.com/gaearon/ffd88b0e4f00b22c3159" rel="noopener noreferrer"&gt;"Redux in 99 lines,"&lt;/a&gt; but here, we’ll walk through the official Redux GitHub repo and create our own minimal Redux implementation. This hands-on approach helps build a strong, practical understanding.&lt;/p&gt;
&lt;h4&gt;
  
  
  Mental Models
&lt;/h4&gt;

&lt;p&gt;Before jumping into code, let’s talk about mental models.&lt;br&gt;&lt;br&gt;
A mental model is simply a way to visualize and understand how something works in your mind.&lt;/p&gt;

&lt;p&gt;If you know Josh Comeau, the teacher behind the &lt;a href="https://www.joyofreact.com/" rel="noopener noreferrer"&gt;"Joy of React"&lt;/a&gt; course, you’ll know he always starts with a mental model before diving into any topic. His explanations, especially about CSS layout algorithms, are inspiring and make complex topics much easier to grasp.&lt;/p&gt;

&lt;p&gt;For example, Josh explains why an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag with &lt;a href="https://stackoverflow.com/questions/5804256/why-does-my-image-inside-a-div-have-extra-space-below-it" rel="noopener noreferrer"&gt;a border shows a weird bottom gap&lt;/a&gt;. This happens because the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag is &lt;code&gt;display: inline&lt;/code&gt; by default. Inline elements are designed for text formatting, so the image is treated like a character in a paragraph, inheriting certain behaviors (like space for descenders). This is also why block-level elements like &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h2&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;h3&amp;gt;&lt;/code&gt;, or &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; have default margins-they’re meant to structure documents.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Visual Example:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Add an image with a border, and you’ll see a gap at the bottom. This is because the image is inline, just like text, and it inherits the same layout rules.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Understanding this mental model makes it clear why some CSS behaviors seem strange at first. Similarly, having a mental model for Redux will help us understand how it works internally and when to use it.&lt;/p&gt;
&lt;h5&gt;
  
  
  What is Redux?
&lt;/h5&gt;

&lt;p&gt;Redux is like &lt;code&gt;useState()&lt;/code&gt;—but for your &lt;strong&gt;entire application&lt;/strong&gt;. It’s that simple.&lt;/p&gt;

&lt;p&gt;Think of Redux as a &lt;strong&gt;global object&lt;/strong&gt; with &lt;code&gt;getter&lt;/code&gt;, &lt;code&gt;setter&lt;/code&gt;, and &lt;strong&gt;pure functions&lt;/strong&gt; that control how values change. Redux gives you a way to store, read, and update data globally in a predictable way.&lt;/p&gt;

&lt;p&gt;In a more abstract form, Redux can be described as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An object (the state),&lt;/li&gt;
&lt;li&gt;A setter (&lt;code&gt;dispatch&lt;/code&gt;),&lt;/li&gt;
&lt;li&gt;A getter (&lt;code&gt;getState&lt;/code&gt;),&lt;/li&gt;
&lt;li&gt;And everything is &lt;strong&gt;pure&lt;/strong&gt; (no side effects, predictable behavior).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Redux allows you to access and manipulate this central object from anywhere in your app.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 01: The Minimal Redux
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = { value: 1 };

function getter() { // Redux calls this getState
    return obj;
}

function setter(d) { // Redux calls this dispatch
    obj.value = d;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is the &lt;strong&gt;most minimalistic&lt;/strong&gt; version of Redux. From here, we’ll evolve it step-by-step into something closer to the actual implementation.&lt;/p&gt;

&lt;p&gt;Before we continue, check out the official Redux documentation. Redux is built on &lt;strong&gt;three core principles&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://redux.js.org/understanding/thinking-in-redux/three-principles#single-source-of-truth" rel="noopener noreferrer"&gt;&lt;strong&gt;Single source of truth&lt;/strong&gt;&lt;/a&gt; — The state of your whole application is stored in a single object tree.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://redux.js.org/understanding/thinking-in-redux/three-principles#state-is-read-only" rel="noopener noreferrer"&gt;&lt;strong&gt;State is read-only&lt;/strong&gt;&lt;/a&gt; — The only way to change the state is to emit an action.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://redux.js.org/understanding/thinking-in-redux/three-principles#state-is-read-only" rel="noopener noreferrer"&gt;&lt;strong&gt;Changes are made with pure functions&lt;/strong&gt;&lt;/a&gt; — Reducers are pure functions that take the previous state and an action, and return the next state.&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;
  
  
  Step 02: Aligning with Redux Concepts
&lt;/h4&gt;

&lt;p&gt;Let’s now apply the three principles to our code, using variable names that align with Redux terminology:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let initialState = { value: 1, name: 'kuyili' };
let currentState = initialState;

function getState() {
    return currentState;
}

function dispatch(d) {
    currentState = { ...d, value: 2 };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We’ve renamed and restructured the variables, but &lt;code&gt;dispatch&lt;/code&gt; is still too simplistic. We need to improve it. Remember: &lt;strong&gt;we’re building a library&lt;/strong&gt;, not just writing app logic.&lt;/p&gt;

&lt;p&gt;When writing app code, you manipulate variables and pass values directly. But in a &lt;strong&gt;library&lt;/strong&gt;, we should focus more on functions. Specifically, functions that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Take functions as arguments&lt;/strong&gt;, and&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Return functions as results&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s call this idea &lt;strong&gt;"Functionable."&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I just explained what &lt;a href="https://github.com/reduxjs/redux/blob/master/src/combineReducers.ts" rel="noopener noreferrer"&gt;&lt;code&gt;combineReducers&lt;/code&gt;&lt;/a&gt; is in Redux. After you finish this article and return to this point, you’ll understand why.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Step 03 – Let's Functionable
&lt;/h4&gt;

&lt;p&gt;We’re now stepping into how Redux really works: by using &lt;strong&gt;functions to manage logic&lt;/strong&gt;, not values. So let’s create a &lt;strong&gt;parent function&lt;/strong&gt; that manages its child functions based on an action type—not by their names, but by using an object like &lt;code&gt;{ type: 'ACTION_NAME' }&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Step 03

function parentFunction(state, action) { // Redux calls her the "reducer"
    switch (action.type) {
        case 'ADD':
            return (); // return updated state here
        case 'SUB':
            return (); // return updated state here
        default:
            return state;
    }
}

function redux() { // Redux calls her the "createStore"

    let initialState = { value: 1, name: 'kuyili' };
    let currentState = initialState;

    function getState() {
        return currentState;
    }

    function dispatch(action) {
        currentState = parentFunction(currentState, action);
        // In Step 04, we’ll add logic here to notify subscribers
    }

    // ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks nice, right?&lt;/p&gt;

&lt;p&gt;But… we're not currently &lt;strong&gt;listening to changes&lt;/strong&gt; in the state. Let's fix that using &lt;strong&gt;closures&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 04 – Closure Magic
&lt;/h4&gt;

&lt;p&gt;This implementation is going to give you a &lt;strong&gt;clean understanding of subscribers&lt;/strong&gt; as well as closures.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures" rel="noopener noreferrer"&gt;Closures are often confusing, right?&lt;/a&gt; Not really.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Closure is like a &lt;strong&gt;power booster&lt;/strong&gt;, a &lt;strong&gt;magic spell&lt;/strong&gt;, or &lt;strong&gt;Popeye’s spinach&lt;/strong&gt; that gives your functions powers almost like a class component—but it still looks like a function.&lt;br&gt;&lt;br&gt;
Think of it like a function that &lt;strong&gt;remembers&lt;/strong&gt; where it came from and everything around it, even when it's executed elsewhere.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using closures, we’ll store subscribers (listener functions) in an array, and expose &lt;code&gt;subscribe&lt;/code&gt; and &lt;code&gt;unsubscribe&lt;/code&gt; features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Step 04

function reducer(state, action) {
    switch (action.type) {
        case 'ADD':
            return { ...state, value: state.value + 1 };
        case 'SUB':
            return { ...state, value: state.value - 1 };
        default:
            return state;
    }
}

function createStore(reducer) {

    let state = { value: 1, name: 'kuyili' };
    let listeners = [];

    function getState() {
        return state;
    }

    function dispatch(action) {
        state = reducer(state, action);
        listeners.forEach(listener =&amp;gt; listener()); // MAGIC: Notify all subscribers
    }

    function subscribe(listener) {
        listeners.push(listener);
        return function unsubscribe() {
            listeners = listeners.filter(l =&amp;gt; l !== listener);
        }
    }

    return {
        getState,
        dispatch,
        subscribe
    };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks great, right?&lt;/p&gt;

&lt;p&gt;To create a subscriber, just call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const unsubscribe = store.subscribe(() =&amp;gt; {
    console.log("State changed!", store.getState());
});

// To unsubscribe later:
unsubscribe();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the &lt;strong&gt;power of closure&lt;/strong&gt;: when you call &lt;code&gt;store.subscribe()&lt;/code&gt;, it returns an &lt;code&gt;unsubscribe()&lt;/code&gt; function that still has access to the original &lt;code&gt;listener&lt;/code&gt; and &lt;code&gt;listeners&lt;/code&gt; array—even after &lt;code&gt;subscribe()&lt;/code&gt; is done executing.&lt;/p&gt;

&lt;p&gt;If you look closely, you'll notice &lt;strong&gt;one thing was missing earlier&lt;/strong&gt;: subscribers were &lt;strong&gt;not notified on dispatch&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We fixed it by simply adding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listeners.forEach(listener =&amp;gt; listener());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time &lt;code&gt;dispatch()&lt;/code&gt; is called (i.e. the state is updated), all subscribed listeners are notified.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 Redux Concepts in a Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;API&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;compose()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Combines all middleware (right to left)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;combineReducers()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Combines all reducers into one&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;middleware&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;You know what this does!&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h4&gt;
  
  
  📂 Stepping into the Redux Codebase
&lt;/h4&gt;

&lt;p&gt;We’re now entering the &lt;strong&gt;actual Redux codebase&lt;/strong&gt; to connect our simplified implementations to real-world source code.&lt;/p&gt;

&lt;h5&gt;
  
  
  🔖 Why Use a Tag Instead of a Branch?
&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;Tags are stable versions. Branches can change, and files like &lt;code&gt;src/index.ts&lt;/code&gt; might be moved or renamed.&lt;br&gt;&lt;br&gt;
At the time of writing, the latest tag is &lt;a href="https://github.com/reduxjs/redux/tree/v5.0.1/src" rel="noopener noreferrer"&gt;&lt;code&gt;v5.0.1&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Start here:&lt;br&gt;&lt;br&gt;
📁 &lt;a href="https://github.com/reduxjs/redux/blob/v5.0.1/src/index.ts" rel="noopener noreferrer"&gt;&lt;code&gt;index.ts&lt;/code&gt;&lt;/a&gt; — The root that exports all Redux APIs.&lt;/p&gt;

&lt;p&gt;You’ll notice we’ve already implemented:&lt;br&gt;&lt;br&gt;
📁 &lt;a href="https://github.com/reduxjs/redux/blob/v5.0.1/src/createStore.ts" rel="noopener noreferrer"&gt;&lt;code&gt;createStore.ts&lt;/code&gt;&lt;/a&gt; — Our minimal version was a very close match!&lt;/p&gt;


&lt;h4&gt;
  
  
  🧩 &lt;code&gt;combineReducers()&lt;/code&gt; – Combining All Reducers
&lt;/h4&gt;

&lt;p&gt;📁 &lt;a href="https://github.com/reduxjs/redux/blob/v5.0.1/src/combineReducers.ts" rel="noopener noreferrer"&gt;&lt;code&gt;combineReducers.ts&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// simplified version
function combineReducers(reducers) {
  return function combination(state = {}, action) {
    const nextState = {};
    let hasChanged = false;

    for (const key in reducers) {
      if (reducers.hasOwnProperty(key)) {
        const previousStateForKey = state[key];
        const nextStateForKey = reducers[key](previousStateForKey, action);
        nextState[key] = nextStateForKey;
        hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
      }
    }

    return hasChanged ? nextState : state;
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔍 What's Going On?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;reducers&lt;/code&gt; is an object of reducer functions.&lt;/li&gt;
&lt;li&gt;For each key:

&lt;ul&gt;
&lt;li&gt;Get the previous state slice.&lt;/li&gt;
&lt;li&gt;Pass it with &lt;code&gt;action&lt;/code&gt; to the corresponding reducer.&lt;/li&gt;
&lt;li&gt;Save the result into &lt;code&gt;nextState&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If any reducer returns a different state, set &lt;code&gt;hasChanged = true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Finally, return the new &lt;code&gt;nextState&lt;/code&gt; &lt;strong&gt;only if it changed&lt;/strong&gt;, else return the original &lt;code&gt;state&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  🔗 &lt;code&gt;compose()&lt;/code&gt; – Middleware Pipeline
&lt;/h4&gt;

&lt;p&gt;📁 &lt;a href="https://github.com/reduxjs/redux/blob/v5.0.1/src/compose.ts" rel="noopener noreferrer"&gt;&lt;code&gt;compose.ts&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// simplified version
function compose(...funcs) {
  if (funcs.length === 0) {
    return (arg) =&amp;gt; arg;
  }

  if (funcs.length === 1) {
    return funcs[0];
  }

  return funcs.reduce((a, b) =&amp;gt; (...args) =&amp;gt; a(b(...args)));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// example
function add5(x) {
  return x + 5;
}

function multiplyBy2(x) {
  return x * 2;
}

function subtract3(x) {
  return x - 3;
}

const composedFunction = compose(subtract3, multiplyBy2, add5);

// Equivalent to: subtract3(multiplyBy2(add5(10)))
console.log(composedFunction(10)); // Output: 27
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  🔍 What's Going On?
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;If no functions → return identity function (&lt;code&gt;x =&amp;gt; x&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;If one function → return that directly&lt;/li&gt;
&lt;li&gt;Else → reduce from right to left using:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(...args) =&amp;gt; a(b(...args)) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the result of &lt;code&gt;b&lt;/code&gt; becomes the input of &lt;code&gt;a&lt;/code&gt;, chaining them right-to-left.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;API&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;createStore()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Core function that holds state and allows dispatching actions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;combineReducers()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Combines multiple reducers into a single state management function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;compose()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Combines middleware functions into a single pipeline (right to left)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There are questions that might help you if you research them yourself, such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How is Redux different from having your own class and using it, like exporting it?&lt;/li&gt;
&lt;li&gt;How is Redux different from an Angular service?&lt;/li&gt;
&lt;li&gt;How is Redux different from &lt;code&gt;localStorage&lt;/code&gt; in a web browser?&lt;/li&gt;
&lt;li&gt;Will this mental model and understanding help me understand the &lt;code&gt;useSelector()&lt;/code&gt; implementation in the &lt;code&gt;react-redux&lt;/code&gt; codebase? &lt;a href="https://github.com/reduxjs/react-redux/blob/master/src/hooks/useSelector.ts" rel="noopener noreferrer"&gt;Here is the link&lt;/a&gt;.
#### What's Next?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After playing with Redux and its codebase, the next step would be to explore React. Honestly, I couldn't understand the React source code very well. I spent more than two weeks trying to get a grasp of it, and I was only able to run the tests in the&lt;a href="https://github.com/facebook/react/tree/main/packages/scheduler" rel="noopener noreferrer"&gt;&lt;code&gt;react-scheduler&lt;/code&gt;&lt;/a&gt; folder. Even then, I couldn't understand the code clearly.&lt;/p&gt;

&lt;p&gt;Anyway, I moved on to the &lt;a href="https://github.com/preactjs/preact" rel="noopener noreferrer"&gt;Preact source code&lt;/a&gt;, which has been really exciting. Why? Because if you go to their codebase, you'll see the implementation is really cool:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/preactjs/preact/blob/10.26.5/hooks/src/index.js#L166" rel="noopener noreferrer"&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/a&gt; is built on top of &lt;a href="https://github.com/preactjs/preact/blob/10.26.5/hooks/src/index.js#L179" rel="noopener noreferrer"&gt;&lt;code&gt;useReducer&lt;/code&gt;&lt;/a&gt; under the hood&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/preactjs/preact/blob/10.26.5/hooks/src/index.js#L307" rel="noopener noreferrer"&gt;&lt;code&gt;useRef&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/preactjs/preact/blob/10.26.5/hooks/src/index.js#L360" rel="noopener noreferrer"&gt;&lt;code&gt;useCallback&lt;/code&gt;&lt;/a&gt; are built on top of &lt;a href="https://github.com/preactjs/preact/blob/10.26.5/hooks/src/index.js#L343" rel="noopener noreferrer"&gt;&lt;code&gt;useMemo&lt;/code&gt;&lt;/a&gt; under the hood&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interesting, right? Simply examining their implementation gives us a deeper understanding of how to use these hooks effectively. The devil is in the details. Happy hacking!&lt;/p&gt;




&lt;p&gt;If any of the information above is incorrect, please let me know at &lt;a href="mailto:inboxtome26@gmail.com"&gt;inboxtome26@gmail.com&lt;/a&gt;, and I'll make the necessary changes.&lt;/p&gt;

</description>
      <category>redux</category>
      <category>statemanagement</category>
      <category>tutorial</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Master Key React Concepts Easily Through One Simple Game</title>
      <dc:creator>Thamizh Arasan</dc:creator>
      <pubDate>Tue, 29 Apr 2025 09:49:47 +0000</pubDate>
      <link>https://dev.to/thamizh_arasan_6f93e2f9b5/master-key-react-concepts-easily-through-one-simple-game-356p</link>
      <guid>https://dev.to/thamizh_arasan_6f93e2f9b5/master-key-react-concepts-easily-through-one-simple-game-356p</guid>
      <description>&lt;p&gt;When starting your journey with React, you'll encounter countless tutorials and project ideas. However, the humble tic-tac-toe game from &lt;a href="https://react.dev/learn" rel="noopener noreferrer"&gt;React's official documentation&lt;/a&gt; isn't just another example—it's a masterclass in React fundamentals disguised as a simple project. Let me show you why this tutorial is the perfect learning experience for any React beginner.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Perfect Microcosm of React Fundamentals
&lt;/h2&gt;

&lt;p&gt;The tic-tac-toe tutorial elegantly introduces nearly every core React concept in a single, approachable project:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. JSX Fundamentals
&lt;/h3&gt;

&lt;p&gt;JSX is React's syntax extension that allows you to write HTML-like code within JavaScript. The tic-tac-toe game gives you hands-on experience with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// JSX example from React's tic-tac-toe tutorial&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Square&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="nx"&gt;onSquareClick&lt;/span&gt; &lt;span class="p"&gt;})&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;(&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;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"square"&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="nx"&gt;onSquareClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&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;button&lt;/span&gt;&lt;span class="p"&gt;&amp;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;/div&gt;



&lt;p&gt;Notice how:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML-like syntax is used within JavaScript&lt;/li&gt;
&lt;li&gt;JavaScript expressions are embedded within curly braces (&lt;code&gt;{value}&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;We use &lt;code&gt;className&lt;/code&gt; instead of &lt;code&gt;class&lt;/code&gt; (a key React convention)&lt;/li&gt;
&lt;li&gt;Event handlers are passed as props (&lt;code&gt;onClick={onSquareClick}&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These fundamentals appear in virtually every React component you'll ever build.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Component Structure and Composition
&lt;/h3&gt;

&lt;p&gt;The game teaches you how to break a UI into logical components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The Board component from the tutorial&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Board&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSquares&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;xIsNext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setXIsNext&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Logic for handling clicks...&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;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"board-row"&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;Square&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;squares&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="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onSquareClick&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;handleClick&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="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;Square&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onSquareClick&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&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;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onSquareClick&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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;
      &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="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* More rows... */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&amp;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;/div&gt;



&lt;p&gt;You'll learn to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create reusable functional components&lt;/li&gt;
&lt;li&gt;Compose larger interfaces from smaller components&lt;/li&gt;
&lt;li&gt;Structure your application logically&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Props and Data Flow
&lt;/h3&gt;

&lt;p&gt;The tutorial beautifully demonstrates React's unidirectional data flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Passing data down through props&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Square&lt;/span&gt; 
  &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;squares&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="si"&gt;}&lt;/span&gt; 
  &lt;span class="na"&gt;onSquareClick&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;handleClick&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="si"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data (&lt;code&gt;value&lt;/code&gt;) flows down from parent to child&lt;/li&gt;
&lt;li&gt;Event handlers are passed down to allow children to communicate up&lt;/li&gt;
&lt;li&gt;Components stay focused on their specific responsibilities&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. State Management with useState Hook
&lt;/h3&gt;

&lt;p&gt;Perhaps the most critical React concept is state management, and the tic-tac-toe game provides perfect practice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// State declarations in the Game component&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setHistory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)]);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentMove&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCurrentMove&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xIsNext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentMove&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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;currentSquares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentMove&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handlePlay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextSquares&lt;/span&gt;&lt;span class="p"&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;nextHistory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&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="nx"&gt;currentMove&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="nx"&gt;nextSquares&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nf"&gt;setHistory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextHistory&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setCurrentMove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextHistory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This demonstrates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating state with &lt;code&gt;useState&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Updating state immutably&lt;/li&gt;
&lt;li&gt;Deriving values from state (&lt;code&gt;xIsNext&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Creating complex state structures (array of arrays)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. Lifting State Up
&lt;/h3&gt;

&lt;p&gt;The tutorial teaches you when and how to "lift state up" to parent components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// In the Game component, state is lifted up from Board&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setHistory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)]);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentMove&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCurrentMove&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="c1"&gt;// ...&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"game"&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="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"game-board"&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;Board&lt;/span&gt; &lt;span class="na"&gt;xIsNext&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;xIsNext&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;currentSquares&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onPlay&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handlePlay&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="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Move history UI... */&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pattern is crucial for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sharing state between components&lt;/li&gt;
&lt;li&gt;Maintaining a single source of truth&lt;/li&gt;
&lt;li&gt;Keeping components in sync&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. Conditional Rendering
&lt;/h3&gt;

&lt;p&gt;The game illustrates how to render content conditionally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Determining and displaying game status&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Winner: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Next player: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xIsNext&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;X&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;O&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&amp;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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;status&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="si"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Board rendering... */&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why This Tutorial Is Exceptional
&lt;/h2&gt;

&lt;p&gt;What makes the tic-tac-toe tutorial uniquely valuable is how it integrates these concepts naturally. Rather than learning them in isolation, you see how they work together in a real (albeit simple) application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Practical Application of Theory
&lt;/h3&gt;

&lt;p&gt;Each concept is immediately applied to solve a real problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State tracks the game board and player turns&lt;/li&gt;
&lt;li&gt;Props pass data and callbacks between components&lt;/li&gt;
&lt;li&gt;Event handlers process user interactions&lt;/li&gt;
&lt;li&gt;Conditional rendering shows game status&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Gradual Complexity
&lt;/h3&gt;

&lt;p&gt;The tutorial builds complexity gradually:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start with a static square component&lt;/li&gt;
&lt;li&gt;Add interactivity with state&lt;/li&gt;
&lt;li&gt;Lift state up for coordination&lt;/li&gt;
&lt;li&gt;Add game history and time travel&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This progression mirrors how you'll build real applications—starting simple and iteratively adding complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking Your Learning Further
&lt;/h2&gt;

&lt;p&gt;After completing the basic tutorial, challenge yourself with these extensions (suggested in the React docs):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Display move locations&lt;/strong&gt;: Enhance the move history to show the coordinates:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example implementation&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;buildMoveDescription&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;move&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;squareIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;move&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Game start&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;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareIndex&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareIndex&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Move #&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;move&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: (&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Highlight winning squares&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Square&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="nx"&gt;isWinningSquare&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onSquareClick&lt;/span&gt; &lt;span class="p"&gt;})&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;(&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;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`square &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;isWinningSquare&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;winning&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="si"&gt;}&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="nx"&gt;onSquareClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&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;button&lt;/span&gt;&lt;span class="p"&gt;&amp;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;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Add a toggle for move order&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isAscending&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsAscending&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Later in render:&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;setIsAscending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isAscending&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;
  &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isAscending&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sort Descending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sort Ascending&lt;/span&gt;&lt;span class="dl"&gt;"&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;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// Then sort moves accordingly:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedMoves&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isAscending&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;moves&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;moves&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion: A Deceptively Powerful Learning Tool
&lt;/h2&gt;

&lt;p&gt;The tic-tac-toe game may seem simple, but it's a brilliantly designed educational tool that covers virtually every fundamental React concept. It strikes the perfect balance—complex enough to teach real-world skills, yet simple enough to complete in a single sitting.&lt;/p&gt;

&lt;p&gt;As you work through this tutorial, you'll gain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Practical experience with React's core concepts&lt;/li&gt;
&lt;li&gt;A mental model for component-based architecture&lt;/li&gt;
&lt;li&gt;The ability to manage state effectively&lt;/li&gt;
&lt;li&gt;Experience with debugging React applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before diving into complex projects or additional libraries like Redux, take the time to thoroughly understand the tic-tac-toe tutorial. The patterns you learn here form the foundation of React development and will serve you in every project you tackle.&lt;/p&gt;

&lt;p&gt;Remember: mastering fundamentals is far more valuable than rushing into advanced topics. The time you invest in understanding this deceptively simple game will pay dividends throughout your React journey.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>🧠 Debugging React Internals: Why I Started Cloning Functions and Objects (Seriously)</title>
      <dc:creator>Thamizh Arasan</dc:creator>
      <pubDate>Tue, 15 Apr 2025 15:25:59 +0000</pubDate>
      <link>https://dev.to/thamizh_arasan_6f93e2f9b5/debugging-react-internals-why-i-started-cloning-functions-and-objects-seriously-507f</link>
      <guid>https://dev.to/thamizh_arasan_6f93e2f9b5/debugging-react-internals-why-i-started-cloning-functions-and-objects-seriously-507f</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“To understand the code, you must *live&lt;/em&gt; in the code.”*&lt;br&gt;&lt;br&gt;
— A React explorer with &lt;code&gt;console.log&lt;/code&gt; as his compass&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Where it all began... 🌱
&lt;/h2&gt;

&lt;p&gt;When I decided to truly understand how React works under the hood — not just use it, but &lt;em&gt;really get it&lt;/em&gt; — I didn’t know where to begin. There was no roadmap. No “Start Here” sign.&lt;/p&gt;

&lt;p&gt;So I did what any curious developer would do.&lt;br&gt;&lt;br&gt;
I opened the React source and started randomly sprinkling &lt;code&gt;console.log()&lt;/code&gt; like confetti 🎉&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;And then... I discovered something curious.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you’ve ever used React, you know we usually begin with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&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;/div&gt;



&lt;p&gt;So I placed a &lt;code&gt;console.log()&lt;/code&gt; right before and after &lt;code&gt;ReactDOM.render()&lt;/code&gt;. And what I saw in the Chrome DevTools console blew my mind 🤯&lt;/p&gt;

&lt;p&gt;It looked like the Fiber tree had already been constructed immediately after calling &lt;code&gt;ReactDOM.render()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But wait... &lt;em&gt;that’s impossible.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I added &lt;code&gt;console.log()&lt;/code&gt; &lt;strong&gt;inside&lt;/strong&gt; the &lt;code&gt;createFiber()&lt;/code&gt; function later — and &lt;strong&gt;that&lt;/strong&gt; was called only afterwards.&lt;/p&gt;

&lt;p&gt;Something didn’t add up.&lt;/p&gt;

&lt;h2&gt;
  
  
  🪞 The Console Illusion
&lt;/h2&gt;

&lt;p&gt;Here’s the trap I fell into — and maybe you have too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, what do you think Chrome’s console shows?&lt;/p&gt;

&lt;p&gt;Just &lt;code&gt;{}&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Nope. You get something like &lt;code&gt;{prop: 123}&lt;/code&gt; with a little blue “i” icon next to it. Why?&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;console.log()&lt;/code&gt; doesn’t snapshot the object at the time of logging. It logs a &lt;em&gt;reference&lt;/em&gt;. The console lazily evaluates and renders it later — &lt;em&gt;after&lt;/em&gt; it’s mutated.&lt;/p&gt;

&lt;p&gt;So all my logs during React Fiber construction?&lt;br&gt;&lt;br&gt;
They showed the &lt;em&gt;state of the object after mutation&lt;/em&gt;, not when the log happened. It was a time-traveling illusion ⏳✨&lt;/p&gt;
&lt;h2&gt;
  
  
  💡 MDN Knows This Too
&lt;/h2&gt;

&lt;p&gt;MDN even talks about this in their docs:&lt;br&gt;&lt;br&gt;
🔗 &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/console#outputting_text_to_the_console" rel="noopener noreferrer"&gt;Outputting text to the console - MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And their suggested workaround?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sure, this works for &lt;strong&gt;plain objects&lt;/strong&gt;. But when you're navigating the jungles of React internals — you're not just dealing with objects.&lt;/p&gt;

&lt;p&gt;You're dealing with &lt;strong&gt;HTMLElements&lt;/strong&gt;, &lt;strong&gt;linked lists&lt;/strong&gt;, &lt;strong&gt;Fibers&lt;/strong&gt;, &lt;strong&gt;Functions&lt;/strong&gt;, and a whole zoo of entities.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧬 When cloning gets serious...
&lt;/h2&gt;

&lt;p&gt;Let’s take HTML elements. &lt;code&gt;JSON.stringify()&lt;/code&gt;? Doesn’t work.&lt;br&gt;&lt;br&gt;
Instead, you might use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cloneNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works better. But what about Fiber nodes? They’re not regular objects. They contain &lt;strong&gt;functions&lt;/strong&gt;, &lt;strong&gt;cyclic references&lt;/strong&gt;, and &lt;strong&gt;complex structures&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That’s where my favorite weapon comes in:  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;lodash.cloneDeep()&lt;/code&gt; 🔥
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cloneDeep&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lodash/cloneDeep&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cloneDeep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fiberNode&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It just works. Deeply. Recursively. Reliably.&lt;br&gt;&lt;br&gt;
It can clone:&lt;/p&gt;

&lt;p&gt;✅ HTMLElements&lt;br&gt;&lt;br&gt;
✅ Functions (to an extent)&lt;br&gt;&lt;br&gt;
✅ Cyclic structures&lt;br&gt;&lt;br&gt;
✅ React's Fiber nodes&lt;/p&gt;

&lt;p&gt;It helped me &lt;strong&gt;capture the actual shape of Fiber&lt;/strong&gt; at the exact moment it was being created — not the shape it had later.&lt;/p&gt;

&lt;p&gt;And when you're debugging the internals of React, that's &lt;em&gt;everything&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛠️ So, when should you clone a function or an object?
&lt;/h2&gt;

&lt;p&gt;Here's the takeaway:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you're debugging or logging, and you care about the &lt;em&gt;exact state at the time of execution&lt;/em&gt;, &lt;strong&gt;clone it&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If you’re dealing with complex data (DOM nodes, Fibers, functions), &lt;code&gt;JSON.stringify()&lt;/code&gt; will &lt;em&gt;fail silently&lt;/em&gt; or just skip important parts.&lt;/li&gt;
&lt;li&gt;For React internals and anything non-trivial: &lt;code&gt;cloneDeep()&lt;/code&gt; is your best friend.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✨ Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Sometimes the tools we use to understand the system can &lt;em&gt;deceive&lt;/em&gt; us.&lt;/p&gt;

&lt;p&gt;React is magical — but to truly learn its magic, you need to look past the illusions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“We don’t get to the truth by looking at the output. We get there by understanding the &lt;em&gt;process&lt;/em&gt;.”&lt;br&gt;&lt;br&gt;
— Me, probably after 10 cups of coffee ☕️&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whether you’re hacking React internals, reverse-engineering Fiber, or just debugging a mysterious bug — &lt;em&gt;clone consciously&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;🧪 Inspect deeply.&lt;br&gt;&lt;br&gt;
🧠 Think like the framework.&lt;br&gt;&lt;br&gt;
🚀 Learn beyond the docs.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>deepdive</category>
      <category>react</category>
      <category>reactjsdevelopment</category>
    </item>
    <item>
      <title>ReactJS Internals #1</title>
      <dc:creator>Thamizh Arasan</dc:creator>
      <pubDate>Mon, 17 Mar 2025 13:15:34 +0000</pubDate>
      <link>https://dev.to/thamizh_arasan_6f93e2f9b5/why-do-we-need-reactjs-or-any-javascript-library-1jog</link>
      <guid>https://dev.to/thamizh_arasan_6f93e2f9b5/why-do-we-need-reactjs-or-any-javascript-library-1jog</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why Do We Need ReactJS or Any JavaScript Library?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When we click on an anchor tag in a traditional website, the browser reloads the entire page. This isn't ideal for SaaS products, where speed and smooth user experience are crucial.&lt;/p&gt;

&lt;p&gt;ReactJS (or similar libraries) solves this by maintaining a virtual representation of the DOM—an object-based structure that mirrors the actual HTML DOM. Instead of reloading the entire page on every interaction, React updates only the necessary parts.&lt;/p&gt;

&lt;p&gt;This optimization is powered by React Fiber, which efficiently reconciles changes and schedules updates. The process of identifying and applying these changes is handled by UnitOfWork(), ensuring that only the modified elements are updated on the screen, keeping performance smooth and efficient.&lt;/p&gt;

&lt;p&gt;This approach makes ReactJS a powerful choice for building dynamic and responsive SaaS applications.&lt;/p&gt;

</description>
      <category>deepdive</category>
      <category>reactinternals</category>
      <category>react</category>
      <category>underthehood</category>
    </item>
  </channel>
</rss>
