<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Willy Liu on Medium]]></title>
        <description><![CDATA[Stories by Willy Liu on Medium]]></description>
        <link>https://medium.com/@willyliu_70611?source=rss-c9e0ff493eb2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*sikNmYc6xP_J7kv-.</url>
            <title>Stories by Willy Liu on Medium</title>
            <link>https://medium.com/@willyliu_70611?source=rss-c9e0ff493eb2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:03:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@willyliu_70611/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding the core concepts of Redux]]></title>
            <link>https://medium.com/@willyliu_70611/understanding-what-redux-actually-is-8fd16ba52043?source=rss-c9e0ff493eb2------2</link>
            <guid isPermaLink="false">https://medium.com/p/8fd16ba52043</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Willy Liu]]></dc:creator>
            <pubDate>Thu, 08 Mar 2018 12:24:07 GMT</pubDate>
            <atom:updated>2020-03-02T15:08:41.565Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zxDn_eIkZ0TTs6T9." /><figcaption>Fitting because React promotes a top down data flow. Photo by <a href="https://unsplash.com/@postrobotbox?utm_source=medium&amp;utm_medium=referral">Ruslan Valeev</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>Disclaimer for anyone who finds this article: I’m not an expert on either React nor Redux, and I’m writing this article mostly for myself to note down my thoughts. Feel free to correct me if anything is completely wrong!</em></p><p>So you start learning React, and you’re getting the hang of all the essential aspects such as components, props and state. Now you’re hearing about Redux and how amazing it is for managing state, but after reading the docs and some other tutorials, you still don’t get it completely.</p><p>Here is how my brain understands it 🤔</p><p><strong>Redux is basically just global state.</strong> Once I understood that part, everything else just sort of clicked in my head. Before explaining what actions, reducers and stores are, we first need to understand why global state is even useful.</p><p>Situation: Let’s say your component tree looks something like this:</p><pre>App<br>|-- Sidebar<br>|   |-- ButtonSection<br>|       |-- IncrementButton<br>|-- MainContent<br>|   |-- InfoPanel<br>|       |-- CounterDisplay<br>|-- Footer<br>    |-- CounterDisplay</pre><p>Yeah this is a weird example but bear with me. In your Sidebar you have a button that increments a counter when clicking it, and in your MainContent , you want to display this value. You also want to display the value in Footer for some reason. We need the counter value in three different places, which aren’t in a direct line of parent/children, so by the principle of <a href="https://reactjs.org/docs/lifting-state-up.html">lifting state up</a>, we put the counter value in their common parent, which happens to be App in this example.</p><p>Notice how even though we only want counter in CounterDisplay , we have to pass it down as a prop through MainContent , InfoPanel , and even Footer on the other branch, just so the value can get down to the right place. These components don’t care about counter at all!</p><p>And in the case of IncrementButton , we need to pass it a callback to increment the actual value, and the callback has to pass through both Sidebar and ButtonSection . Again, these components don’t need that callback at all! Now imagine if, instead of just 2 levels deep, you have even more nested components 😫</p><p>Wouldn’t it be better if the components that actually cared could get those values directly, without having to pass them down all the way?</p><p>As it happens, this is exactly the type of problem that global state can solve 🤯</p><p>Keep in mind that it’s not always useful or desirable to keep <em>all </em>state in the global state — you should limit yourself to only storing <em>application state</em> which is actually needed globally by your app. This could be for example the logged in user. UI state (e.g. isOpen for a drop down list component) is probably not needed globally for your application, so this can just live inside regular React component state.</p><h4>Where state is stored</h4><p>So how does that work? Firstly, our state is kept globally in a so called store, instead of being tied to a particular component. When we want to read from our state (which is immutable), we need to connect our component that needs the state to the store.</p><p>Connecting is like “okay, I have my state globally, but I want <em>this </em>component to be able to access it (in the form of props), no matter where in the component tree is”. Thus, we can bypass having to pass the counter value down, we just connect the CounterDisplay component to our store .</p><h4>How to modify state</h4><p>Right, so I just said that state is immutable, so how do you change it? Instead of being able to change the state directly (as in this.setState() in React), you have to “dispatch” actions . An action is, simply put, an object that describes what just happened.</p><p>For example, we could define that theIncrementButton component dispatches an action of the type INCREMENT_COUNTER when clicking on it. This action is sent (dispatched) to the store, and it’s up to you to decide how to interpret actions and how they change the state.</p><p>What does “dispatch” mean? When you connect a component to the store, the component will receive a function prop called dispatch. To dispatch an action, you simply need to call dispatch(someAction). For the example above, you could write this as:</p><pre>this.props.dispatch({<br> type: INCREMENT_COUNTER,<br>});</pre><h4>Interpreting actions</h4><p>So now you understand that you can never modify the state directly, only dispatch actions. When you first create your store, you need to pass it a reducer , which is just a function that, given the state and an action, returns a new state, and is called every time an action is dispatched.</p><p>Reducers are really simple to grasp if you see an example:</p><pre>function reducer(state = 0, action) {<br>  switch (action.type) {<br>    case INCREMENT_COUNTER:<br>       return state + 1;<br>    default:<br>       return state;<br>  }<br>}</pre><p>So our state here is just a number, which defaults to 0. When the reducer sees an action of the type INCREMENT_COUNTER we have designed it so that it simply returns the old value + 1. And that’s all a reducer is, a function that takes the state and an action and decides how to interpret the action into a new state.</p><h4>Summary</h4><ul><li>“Global”, immutable state, stored in the store</li><li>You can connect a component to the store to read the state directly, without having to pass down props from the root component</li><li>You dispatch actions to the store in order to change the state</li><li>Reducers receive the actions and decide how the action will give you your next state</li></ul><h4>That’s it?</h4><p>There are tons of other stuff that you can read about such as middleware, time travel debugging etc. Definitely check out something like Redux Thunk or Redux Saga if you want to do async stuff with Redux.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8fd16ba52043" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>