<?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: Felipe Gustavo</title>
    <description>The latest articles on DEV Community by Felipe Gustavo (@felipegs).</description>
    <link>https://dev.to/felipegs</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%2F259484%2F2beab69c-a3dc-4045-8bc0-5a14874cae9c.jpg</url>
      <title>DEV Community: Felipe Gustavo</title>
      <link>https://dev.to/felipegs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/felipegs"/>
    <language>en</language>
    <item>
      <title>Why Zustand suceed in the React state management war?</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Wed, 22 Jan 2025 17:27:39 +0000</pubDate>
      <link>https://dev.to/felipegs/why-zustand-suceed-in-the-react-state-management-war-2e45</link>
      <guid>https://dev.to/felipegs/why-zustand-suceed-in-the-react-state-management-war-2e45</guid>
      <description>&lt;p&gt;Zustand reached 49 thousand stars on github last week, which got everybody impressed.&lt;/p&gt;

&lt;p&gt;In the last years we saw a change in the state management field, especially in React. The older champion, redux, is slowly becoming a legacy only library, not being so popular like it was.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redux era
&lt;/h2&gt;

&lt;p&gt;Redux had the tragedy of being in the right place at the right time, but maybe, not in the best shape. It was the only library that remained from the Flux War, and became the default library on React, especially in the class-based applications era.&lt;/p&gt;

&lt;p&gt;Their success was good and evil for them. Because more and more, applications that didn’t need a library like redux were using it. And its rawness made everybody stick to huge boilerplates to structure their states.&lt;/p&gt;

&lt;p&gt;I said the wrong shape, because I think that, if &lt;a href="https://redux-toolkit.js.org" rel="noopener noreferrer"&gt;redux-toolkit&lt;/a&gt; was the version people used a lot in the past, their views on redux would be different today. Being more positive, than it is.&lt;/p&gt;

&lt;p&gt;And &lt;a href="https://github.com/pmndrs/zustand" rel="noopener noreferrer"&gt;here we come to Zustand&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  The new champion
&lt;/h2&gt;

&lt;p&gt;Zustand is &lt;a href="https://github.com/pmndrs" rel="noopener noreferrer"&gt;maintained by Poimandres&lt;/a&gt; and their lead maintainer is &lt;a href="https://blog.axlight.com" rel="noopener noreferrer"&gt;Daishi Kato&lt;/a&gt;. The interesting part of it is that they have not just one, but three state management solutions: Zustand, Jotai and Valtio.&lt;/p&gt;

&lt;p&gt;Valtio is the one with least success of the three, but Jotai and Zustand, are, of course, the two bigger ones of the new generation, &lt;a href="https://blog.axlight.com/posts/when-i-use-valtio-and-when-i-use-jotai/" rel="noopener noreferrer"&gt;even though they are VERY different from one another&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Jotai is an atom-based library, with very design inspiration on reactivity. &lt;a href="http://localhost:3000/series/reactivity-react" rel="noopener noreferrer"&gt;We have a series on reactivity in react, by the way&lt;/a&gt;. With a simple and minimalist API, it’s today, the best atom-based tool for React.&lt;/p&gt;

&lt;p&gt;But being an atom-based library is not easy. As we know, people still struggle with reactivity and the atom model can be pretty strange for the users that are used to using bigger data structures to handle their data.&lt;/p&gt;

&lt;p&gt;That makes the adoption to Jotai harder, and here is the point where Zustand shines the most.&lt;/p&gt;

&lt;p&gt;Think that, here in the future, we could look to the past and create a better version of something? This is the sensation &lt;a href="https://blog.axlight.com/posts/how-zustand-was-born/" rel="noopener noreferrer"&gt;I had looking at Zustand&lt;/a&gt;. Seems like a new version of redux, with a refined and minimalist API, joining things that were splitted (like state and actions) and delivering &lt;a href="https://blog.axlight.com/posts/why-zustand-typescript-implementation-is-so-ugly/" rel="noopener noreferrer"&gt;good typescript support&lt;/a&gt; and a powerful middleware system.&lt;/p&gt;

&lt;p&gt;These two aspects are very important to me. When you choose a state solution to your application, you may need to extend some functionalities. &lt;a href="https://redux.js.org/understanding/history-and-design/middleware" rel="noopener noreferrer"&gt;Redux had a good middleware system&lt;/a&gt;, but more complex. Zustand got it right.&lt;/p&gt;

&lt;p&gt;Another important difference is the multi-store approach, instead of gigantic states, on Redux. This way you avoid the creation of big reducer combinations and can organize better your architecture, splitting the real global states from page/feature local states, that benefits of being outside of React, without being in a huge blob of redux.&lt;/p&gt;

&lt;p&gt;So this makes the success of Zustand not a surprise for me. For the user, it follows the natural React way of thinking, and at the same time seems like a cleaner option if you don’t want to fall in the hell of Contexts (that are so verbose and confused such as old redux was, even its adopters say the opposite).&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>The problem with usePrevious and similar time oriented hooks</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Mon, 16 Dec 2024 18:47:36 +0000</pubDate>
      <link>https://dev.to/felipegs/the-problem-with-useprevious-and-similar-time-oriented-hooks-7ij</link>
      <guid>https://dev.to/felipegs/the-problem-with-useprevious-and-similar-time-oriented-hooks-7ij</guid>
      <description>&lt;p&gt;Theo did a &lt;a href="https://www.youtube.com/watch?v=B-Xb_8n5wRg" rel="noopener noreferrer"&gt;video&lt;/a&gt; this week about unintuitive behaviors of React hooks, exploring especially the idea of a hook called &lt;code&gt;usePrevious&lt;/code&gt;, to keep the value version of the last re-render, before the current one. A way of doing some logic with the old and new state.&lt;/p&gt;

&lt;p&gt;If you want to see ideas of how to implement it, please check the video, in this post the idea is to explore the conceptual aspect of having a hook like &lt;code&gt;usePrevious&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactive expressions
&lt;/h2&gt;

&lt;p&gt;As you can see, we don’t have a primitive hook like this in React. Before hooks, in the class-based era, we had a lifecycle method called &lt;a href="https://react.dev/reference/react/Component#componentdidupdate" rel="noopener noreferrer"&gt;componentDidUpdate&lt;/a&gt; where we got all the previous state and props as parameters, why didn't they keep this behavior with hooks?&lt;/p&gt;

&lt;p&gt;It can be a little repetitive if you are reading this series of posts, but we need to talk about the &lt;a href="https://www.felgus.dev/blog/useeffect-wrong-usage#paradigm-mental-shift" rel="noopener noreferrer"&gt;paradigm shift&lt;/a&gt;, in this case.&lt;/p&gt;

&lt;p&gt;With classes, when some state updates, you don’t have an automatic way of recalculating the derived values. If you are using some specific props and states to calculate some new value, you need to verify by yourself if some of them have changed.&lt;/p&gt;

&lt;p&gt;This way, the solution is to have a callback that is called in all updates, and send to the user the previous values. The app code checks the differences and updates the calculated state with the new result. This is the directness of class-based components, you have complete control of the flow of data, and need to manually control the calculations.&lt;/p&gt;

&lt;p&gt;Here we come to reactivity expressions.&lt;/p&gt;

&lt;p&gt;Instead of having to check and do the change, you write an expression, the calculation formula, sort of. This calculation needs to be executed with the current state version, without access to the previous one.&lt;/p&gt;

&lt;p&gt;Imagine a formula:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;


&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;


&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I use this expression 1 million times, passing b as 10 and c as 20, I will get the same result. This is a pure calculation. React performs the same principle. All calculations of derivations should be pure.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But why it matters?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;React work in re-renders. Each cycle generates a description of the UI and based on the differences between the current one and the next one, it commits changes to the DOM. Each render is completely separated from the previous or the next.&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;UI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So for each different state version, we got a different UI version. This becomes pretty confusing if we add previous values here. Because now it does not just depend on the state, but on the previousState as well. Instead of having one source, one expression and one result, I can have multiple sources, maybe more complex expressions to handle these sources and inconsistent and unpredictable UI as result.&lt;/p&gt;

&lt;p&gt;Each render will behave differently based on the previous state. And as some of the possible implementations of &lt;code&gt;usePrevious&lt;/code&gt; rely on time ordering in React, this becomes more dangerous.&lt;/p&gt;

&lt;p&gt;With concurrent features, React can stop without warning a render, to prioritize other actions. Depending on &lt;code&gt;useEffect&lt;/code&gt; and ref can make you keep a stale version of a “previous” render that is even the real previous one. More mess to reason about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memoization
&lt;/h2&gt;

&lt;p&gt;Think in an expression like this&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One part of that has priority and needs to be calculated before, let’s think it with javascript code:&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;cdResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;d&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cdResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now we have two separated expressions that can be calculated separately, and are perfectly pure. But if the value of b changes a lot and the calculation for cdResult is expensive, how can we solve it? Memorizing!&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;cdResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cdResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;cdResult&lt;/code&gt; will just be recalculated if c or d changes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But above in the text I said there is no previous value, but how can a calculation of one render be used in the next one? This doesn't break the purity of calculations?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Actually, no. For example:&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;// render 1&lt;/span&gt;
&lt;span class="c1"&gt;// c = 30; d = 20&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cdResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// = 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine we are in the render number 1. The c has the value of 30 and d has the value of 20, so the result is 10. But as I am memoizing it, React will keep track of the dependencies I added on the array. If some of them change, it recalculates.&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;// render 2&lt;/span&gt;
&lt;span class="c1"&gt;//  c = 30; d = 20&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cdResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// = 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But they didn’t change. If I call this expression again, with c as 30 and d as 20, I will get the same 10 as result. Even though I am in the render number 2 and other variables have changed, the dependencies I use in this calculation didn’t change.&lt;/p&gt;

&lt;p&gt;I can calculate it again in each render, it’s the default behavior of React, but I can choose to skip an unnecessary recalculation that will return the same value, so I kept it. We kept the purity and we kept the separation between renders&lt;/p&gt;

&lt;h2&gt;
  
  
  Previous state
&lt;/h2&gt;

&lt;p&gt;But there is a good place to do logic with previous state, user actions. Of course, that in the moment the callback is called, that would be the current state. But if you have some state that needs to change based on some logic, it’s the place.&lt;/p&gt;

&lt;p&gt;Of course it can have very specific cases where maybe you need a hook such as &lt;code&gt;usePrevious&lt;/code&gt;, but be aware of the inconsistencies it can cause, and try to add guarantees to avoid bugs on the application.&lt;/p&gt;

&lt;p&gt;And more importantly, if possible, avoid it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>reactnative</category>
    </item>
    <item>
      <title>Reasoning about the useEffect wrong usage</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Fri, 06 Dec 2024 23:38:17 +0000</pubDate>
      <link>https://dev.to/felipegs/reasoning-about-the-useeffect-wrong-usage-9of</link>
      <guid>https://dev.to/felipegs/reasoning-about-the-useeffect-wrong-usage-9of</guid>
      <description>&lt;p&gt;The most wrong used hook in React is, for sure, &lt;code&gt;useEffect&lt;/code&gt;. We have multiple reasons for this, not just one. Let’s explore each one of them, from my point of view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lifecycle legacy
&lt;/h2&gt;

&lt;p&gt;So one of the reasons I think has more impact is that, in the pre-hooks era, we used classes. For those who started to use React in this period, it’s used to use lifecycle methods and this.state. &lt;a href="https://www.felgus.dev/blog/life-cycles-in-react-hooks#old-but-gold-classes" rel="noopener noreferrer"&gt;I wrote a little about this in this post&lt;/a&gt;. There are people that miss the old but gold classes period, and value its simplicity and directness. It’s a model that fits pretty well with the common knowledge of programmers in general, that learn object oriented and imperative programming, and the mental structure just connects with that model.&lt;/p&gt;

&lt;p&gt;Then they added hooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Paradigm mental shift
&lt;/h2&gt;

&lt;p&gt;The problem is the paradigm shift that happened. Programmers in general are very familiar with the imperative and the object oriented paradigms, they are commonly teached on colleges and courses, mainly the imperative one, follows the common flow of thinking of a human being.&lt;/p&gt;

&lt;p&gt;When you switch to different paradigms like functional programming, you face a reversed way of thinking, that is not so close to the common one. This “reversion” makes it more difficult to understand.&lt;/p&gt;

&lt;p&gt;Reactive programming suffers the same issue. It’s a change from an active to a passive way of doing programming. We see it with the wrong usage of &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Most of the &lt;strong&gt;"ERRORS"&lt;/strong&gt; are state syncing. So, the dev uses the &lt;code&gt;useEffect&lt;/code&gt; to track some state or prop and based on some logic, to change the state. This case reveals the opposite way of thinking we need here.&lt;/p&gt;

&lt;p&gt;In OOP and imperative programming, you are active, doing the changes and logic in a proactive way. Reactivity is based on the opposite, you react to a chance, and declare the calculations you want the system to do when the state changes.&lt;/p&gt;

&lt;p&gt;For most of the users, to actively set the new state on the &lt;code&gt;useEffect&lt;/code&gt; is the direct way, the state changed, so you need to manually track the change and update other states with it. The docs say it’s NOT recommended, but there is no clear reason for this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.felgus.dev/blog/react-relation-to-derivation" rel="noopener noreferrer"&gt;To derive in React is the recommended way&lt;/a&gt; not just for performance reasons, but for a conceptual one. React is a derivation machine, and the result, in the end, is the derivation of the UI. You don’t need to actively handle these state transitions and recalculations, it just happens, based on the declarative code you wrote.&lt;/p&gt;

&lt;p&gt;React docs didn’t explain this very well, after hooks, React core team and content creators didn’t make talks or courses explaining these concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  React’s conceptual confusion
&lt;/h2&gt;

&lt;p&gt;React seems to have a “conceptual confusion”, the transition to hooks is the most strong example of it, but not the only one. There is one big difference on the usage of hooks, it’s based on reactivity, and even though the React core team jokes about reactivity, it was their decision to switch to it.&lt;/p&gt;

&lt;p&gt;Functional components are perfect for it. Each re-render calls again the function, and everything inside gets the state and props current version, so, everything created inside behaves like a derivation. The return, the JSX, is the derivation of the UI.&lt;/p&gt;

&lt;p&gt;But React is not the perfect and pure implementation of functional programming and reactivity. It takes concepts and ideas as inspiration and merges them creating their own model, but the core is there, anyway. &lt;/p&gt;

&lt;p&gt;And this needs to be clear. Even without being the example of reactivity, it uses their concepts, and to know deeper the patterns make it easy for a developer to think and create solutions with those primitives. That’s why I’m writing this “Reactivity in React” series, by the way.&lt;/p&gt;

&lt;p&gt;Not just say to the users, “don’t sync state on &lt;code&gt;useEffect&lt;/code&gt;, it’s bad”, but explain why it’s bad and how to think in a way that “sync state” is even the first solution thought.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lack of some primitives
&lt;/h2&gt;

&lt;p&gt;This cause is one that is improving, especially in React 19. &lt;a href="https://www.felgus.dev/blog/async-derivations" rel="noopener noreferrer"&gt;Async derivations&lt;/a&gt; was one of the causes to use &lt;code&gt;useEffect&lt;/code&gt;, but now we will have to use primitive, which fills this gap in some ways.&lt;/p&gt;

&lt;p&gt;Of course, we still have some weak spots in the primitives, such as the case for &lt;a href="https://www.felgus.dev/blog/dynamic-derivations" rel="noopener noreferrer"&gt;dynamic derivations&lt;/a&gt;, and other cases, &lt;a href="https://react.dev/blog/2024/12/05/react-19#cleanup-functions-for-refs" rel="noopener noreferrer"&gt;but more and more&lt;/a&gt; React moves more the side effects out of hooks field, as this case of ref callbacks. &lt;/p&gt;

&lt;p&gt;And we can always hope for news in the future. I invite everyone to read all other &lt;a href="https://www.felgus.dev/series/reactivity-react" rel="noopener noreferrer"&gt;Reactivity in React&lt;/a&gt; posts and bring specific cases and questions, we can explore and find more solutions to these common problems with reactivity.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>The death of useEffect and the dynamic derivations role in it</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Wed, 27 Nov 2024 13:44:10 +0000</pubDate>
      <link>https://dev.to/felipegs/the-death-of-useeffect-and-the-dynamic-derivations-role-in-it-325g</link>
      <guid>https://dev.to/felipegs/the-death-of-useeffect-and-the-dynamic-derivations-role-in-it-325g</guid>
      <description>&lt;p&gt;The title is a little “click bait”, but the idea is not wrong.&lt;/p&gt;

&lt;p&gt;I don’t have the opinion that &lt;code&gt;useEffect&lt;/code&gt; is a bad hook, actually it is quite the opposite. But in React, which works as a &lt;a href="https://www.felgus.dev/blog/react-relation-to-derivation" rel="noopener noreferrer"&gt;derivation machine&lt;/a&gt;, to use &lt;code&gt;useEffect&lt;/code&gt; for state sync is not the best option, and &lt;a href="https://react.dev/learn/you-might-not-need-an-effect" rel="noopener noreferrer"&gt;not recommended by the core team&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  But wait, and if I have the case where I need to sync a state?
&lt;/h3&gt;

&lt;p&gt;Yes, it’s very rare cases and perhaps there are better solution options, but imagine we have a post in a app and we want to have a local state to handle optimistic updates, when the user clicks on like button, but that it syncs with external state (coming from backend) to really know if the like happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mutable derivations
&lt;/h2&gt;

&lt;p&gt;This is a case where we need a primitive where we can do state and derivation at the same time. Conceptually speaking, we can think of it as a mutable derivation. &lt;a href="https://dev.to/this-is-learning/mutable-derivations-in-reactivity-2ffl"&gt;Ryan Carniato has a good blog post about it&lt;/a&gt;, but there are some limitations related with React way of managing reactivity, but this new primitive would be derivation and state in just one:&lt;br&gt;
Derivation as the default behavior, getting the last updated source of truth (the data prop coming from server).&lt;br&gt;
State when the user clicks on the like button. This change is temporary and will be overwritten on the next dependencies change.&lt;/p&gt;

&lt;p&gt;We don’t have it on React, of course, but we can imagine a primitive like that. The positive points of it: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it removes one more use case from useEffect.&lt;/li&gt;
&lt;li&gt;it makes clear that we have a derivation that can be handled as a state.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  useMemoState
&lt;/h2&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;internalState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInternalState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemoState&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The hook accepts two arguments like a normal &lt;code&gt;useMemo&lt;/code&gt;, being the first one the memo function, that would execute on the first time normally, the returned value will be the state. The second argument is the well known array of dependencies, to keep track of the values and re-calculate the memo if necessary. It returns a tuple with two values, the state/memo value and the setter that would change the state temporarily.&lt;/p&gt;

&lt;p&gt;Using the hook would be like this example:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MemoState&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;data&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="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;internalState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setInternalState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemoState&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&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;section&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;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;name: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;internalState&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;title&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;p&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;p&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;internalState&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;liked post&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;not liked post&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;p&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;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="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// likePost();&lt;/span&gt;
        &lt;span class="nf"&gt;setInternalState&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="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="na"&gt;like&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&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;like&lt;/span&gt;
        &lt;span class="p"&gt;}))&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;Like Button&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;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;section&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 way you remove the need of a &lt;code&gt;useEffect&lt;/code&gt; here, knowing exactly what this hook is doing and is capable of, and fill this conceptual gap, where our problem needs a state and a derivation at the same time. Ironically, it removes the need for an effect as a consequence.&lt;/p&gt;

&lt;h3&gt;
  
  
  How can we do something similar in React?
&lt;/h3&gt;

&lt;p&gt;In order to follow React rules and keep the hook safe, we will need to use &lt;code&gt;useEffect&lt;/code&gt; to create something similar in today’s React. I don’t think a hook like that will be made in the future, but who knows, right?&lt;/p&gt;

&lt;p&gt;To avoid the usage of &lt;code&gt;useEffect&lt;/code&gt; here, we will need to use some external code like a mini state library to handle it, or if we want to keep strict React primitives, to use useRef to keep sync of values.&lt;/p&gt;

&lt;p&gt;The problem with this approach is that it breaks React rules, and &lt;a href="https://react.dev/reference/react/useRef#usage" rel="noopener noreferrer"&gt;it’s not recommended by the docs&lt;/a&gt;. Because the only way for it to work without the effect, would be to sync the refs during the render of the component, not safe at all.&lt;/p&gt;

&lt;p&gt;So, to keep it simple and safe, a example of hook would be like that:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useMemoState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;memoFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;depsList&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="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;setState&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;memoFn&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;memoFn&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;depsList&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="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&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;



</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Lifecycle doesn't exist in React with hooks</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Tue, 12 Nov 2024 14:08:24 +0000</pubDate>
      <link>https://dev.to/felipegs/lifecycle-doesnt-exist-in-react-with-hooks-2cnm</link>
      <guid>https://dev.to/felipegs/lifecycle-doesnt-exist-in-react-with-hooks-2cnm</guid>
      <description>&lt;p&gt;A long long long time ago, we used React with classes, remember?&lt;/p&gt;

&lt;p&gt;At that time, we had the concept of lifecycle methods, methods on the classes that accepted callbacks that would be executed in certain moments. The big three: on mount, on update and on unmount.&lt;/p&gt;

&lt;h2&gt;
  
  
  Old, but gold classes
&lt;/h2&gt;

&lt;p&gt;That was important, on the classes components the returned JSX was made on render method, the state attached to the this of the component, and the app developer needed a way to know to do actions in certain moments. We had the idea of the time on the life of a component:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;componentDidMount&lt;/code&gt; is the moment the component renders for the first time and adds elements to the DOM and is the moment to start connection and side effects such as API requests.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shouldComponentUpdate&lt;/code&gt; allows you to manually set your logic to compare the next props and state and return a boolean to define if the re-render could be skipped or not.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;componentDidUpdate&lt;/code&gt; is the moment the state or props changed, calling render method again and doing the reconciliation changes to identity difference and apply to the DOM, good to sync state with new props and do logic stuff.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;componentWillUnmount&lt;/code&gt; is when React would remove the elements from the DOM and was a good place to clean things and avoid memory leaks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And of course, you had an important API such as &lt;code&gt;forceUpdate&lt;/code&gt;, that allowed you to manually trigger a &lt;strong&gt;re-render&lt;/strong&gt; if you are using external data that would not connect with React state updates.&lt;/p&gt;

&lt;p&gt;At a conceptual level, we have a more direct way of conducting the flow of the app. The lifecycle methods followed a similar life cycle of a DOM element, you could do &lt;code&gt;memo&lt;/code&gt; and &lt;code&gt;forceUpdates&lt;/code&gt; by yourself, syncing state was the default way of doing logic.&lt;/p&gt;

&lt;p&gt;This directness was seen as simplicity, and to learn these concepts was easier compared with the reactive model. But then, hooks arrived and changed everything.&lt;/p&gt;

&lt;h2&gt;
  
  
  The unnamed reactivity
&lt;/h2&gt;

&lt;p&gt;The transition was confusing. First, in a search to make it easy, and sort of, maintain the conceptual vision of React model that devs had, a lot of communications tried to show the similarities on the hooks model. To have the 3 main life cycles methods, they showed workarounds with &lt;code&gt;useEffect&lt;/code&gt;.&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;// componentDidMount&lt;/span&gt;
 &lt;span class="nf"&gt;useEffect&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="c1"&gt;// code...&lt;/span&gt;

    &lt;span class="c1"&gt;// componentWillUnmount:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// code...&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="c1"&gt;// componentDidUpdate&lt;/span&gt;
 &lt;span class="nf"&gt;useEffect&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="c1"&gt;// code...&lt;/span&gt;

  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependencyState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dependencyProp&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, most of the new React code made with hooks followed this idea, and starting to sync state was a natural process. In order to keep the same idea of lifecycle methods, it was the place to call setState and trigger the re-render process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What is the problem with it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Syncing state became a problem, the wrong usage of &lt;code&gt;useEffect&lt;/code&gt; became a problem, double re-renders became a problem, too much re-renders became a problem, performance became a problem.&lt;/p&gt;

&lt;p&gt;It’s a little bit confusing this step from React, at least for me. Because, the move to hooks was a move to a reactive model, even if it’s a coarse-grained one. But the communication was that nothing really big changed. No content about the reactivity concepts and theory, even working for years with React, I just started to really understand reactivity reading &lt;a href="https://dev.to/ryansolid"&gt;Ryan Carniato’s blog&lt;/a&gt; posts about reactivity and &lt;a href="https://dev.to/ryansolid"&gt;solid&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Even knowing that &lt;code&gt;useEffect&lt;/code&gt; had a misuse, I really didn’t understand why, and this lack of conceptual theory about reactivity makes committing mistakes with hooks so easy. &lt;code&gt;useEffect&lt;/code&gt; became the most hated hook, being called &lt;strong&gt;“useFootgun”&lt;/strong&gt; for some people. The point is, there is a conceptual confusion in React that expresses itself as all the issues with &lt;code&gt;useEffect&lt;/code&gt; we see today.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useEffect issues are not the cause of the problem, but the consequence.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What about life cycle with hooks
&lt;/h2&gt;

&lt;p&gt;So, this is the thing. There is no life cycle in the concept of reactivity.&lt;/p&gt;

&lt;p&gt;You have a change, you react to it deriving and doing side effects. Effects are the consequence, not the cause. There is no state sync and no concepts of mount and unmount.&lt;/p&gt;

&lt;p&gt;It should not matter if it is the first, the 10th or the last render before unmount, and the hooks don’t care for it, by the way, even &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Try it:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;EffectExample&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="nf"&gt;useEffect&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;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;effect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&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="o"&gt;=&amp;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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clean up&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;count&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;span class="nx"&gt;count&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;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;setCount&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="nx"&gt;state&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="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;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;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;You will see on your &lt;code&gt;console&lt;/code&gt; both functions being executed on each state update. First the clean up one, and then the effect callback. If you are using &lt;code&gt;useEffect&lt;/code&gt; with some state or prop to do a subscription, every time the dependencies changes, the clean up function will be called, and then the new callback, doing the subscription again, but with the new values.&lt;/p&gt;

&lt;p&gt;You should look your app code as the React model simplified:&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;UI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have a component like this one:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="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;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;setCount&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="nx"&gt;state&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="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;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;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;what you really have, when you click on the button and adds +1 to the count, &lt;strong&gt;conceptually&lt;/strong&gt;, is something like this:&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;// first 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="p"&gt;&amp;gt;&lt;/span&gt;0&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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;// button click - re-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="p"&gt;&amp;gt;&lt;/span&gt;1&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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="c1"&gt;// button click - re-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="p"&gt;&amp;gt;&lt;/span&gt;2&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&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="c1"&gt;// button click - re-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="p"&gt;&amp;gt;&lt;/span&gt;3&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each click calls again the fn, with a new state, generating a new version of UI. The state should change by the action of the user or by an async value that should be made with &lt;a href="https://www.felgus.dev/blog/async-derivations" rel="noopener noreferrer"&gt;async derivations&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This way you keep the clean idea:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;state transitions make a new fn call&lt;/li&gt;
&lt;li&gt;with the new state, you get the UI description&lt;/li&gt;
&lt;li&gt;if it’s different, update the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A clean and consistent model.
&lt;/h2&gt;

&lt;p&gt;it’s a matter of the renderer to care with adding, updating and removing elements from the screen. At the component level, what matters is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the state changed&lt;/li&gt;
&lt;li&gt;if the app can handle the user actions&lt;/li&gt;
&lt;li&gt;the returned structure in the JSX.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hooks and its reactive model make React decouple itself from the browser, making the app code care less about in which moment of screen rendering process you are. You don’t force updates and even handle memos by your own rules anymore, it's less direct for the app dev, but more direct in terms of model.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Each re-render generates a structure, React takes care of the rest.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Async derivations in React</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Thu, 07 Nov 2024 02:37:43 +0000</pubDate>
      <link>https://dev.to/felipegs/async-derivations-in-react-4jop</link>
      <guid>https://dev.to/felipegs/async-derivations-in-react-4jop</guid>
      <description>&lt;p&gt;We have a problem, &lt;a href="https://dev.to/this-is-learning/async-derivations-in-reactivity-ec5"&gt;async is hard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Imagine you have a simple GET API, a search that receives as param a &lt;code&gt;searchText&lt;/code&gt;. You call it using your http request tool of preference and get a promise, it resolves to the list of whatever you are searching for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can I call it in a React component?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First of all, it’s important to notice one thing, what I described can be modeled as:&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;searchAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s get conceptual here. &lt;strong&gt;This is a derivation&lt;/strong&gt;. For each version of &lt;code&gt;searchText&lt;/code&gt;, you can get a different result. But there are some problems here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it 's external data.&lt;/li&gt;
&lt;li&gt;it returns a promise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How can I call it as a derivation in React?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using third-party libraries, such as &lt;a href="https://tanstack.com/query/v3" rel="noopener noreferrer"&gt;TanStack Query&lt;/a&gt; and &lt;a href="https://swr.vercel.app/pt-BR" rel="noopener noreferrer"&gt;SWR&lt;/a&gt; solves our problem. They give us hooks that we can use in a React component, receiving our states and props and recalculating (refetching) to the API when it changes. Look this example:&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;searchResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useQuery&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;queryKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;search&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;searchText&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="na"&gt;queryFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;getSearch&lt;/span&gt;&lt;span class="p"&gt;,});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ok, we solved async derivation, right?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not really.&lt;/p&gt;

&lt;p&gt;By the way, I always recommend just using one of these libraries, they are awesome and save a ton of time with more complex cases (such as refetching, retry, cache control and etc), but we cannot count on a third-party to solve a conceptual problem of React.&lt;/p&gt;

&lt;p&gt;Coming again to reactivity, we need to have a way of dealing with asynchronous cases in a derivation model. React should give us a primitive for this case. Well, until version 18 we didn’t have it, but &lt;a href="https://19.react.dev/reference/react/use" rel="noopener noreferrer"&gt;in 19 it’s different.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The ‘use’ case
&lt;/h2&gt;

&lt;p&gt;React 19 introduces a new primitive called &lt;code&gt;use&lt;/code&gt;. Yes, the naming is a little confusing, but its role on the reactivity model of React is pretty important. With it, we can solve promises during the render of a component. &lt;strong&gt;The missing derivation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before it, the only way of calling a fetch during the render of a component was to use &lt;code&gt;useEffect&lt;/code&gt;, call the &lt;code&gt;promise&lt;/code&gt; and on the &lt;code&gt;then&lt;/code&gt; clause, set a state with the value that comes as response. That worked, sort of, but we had &lt;a href="https://www.felgus.dev/blog/react-relation-to-derivation#derivation-machine" rel="noopener noreferrer"&gt;all the problems of using an effect to do it&lt;/a&gt;.&lt;br&gt;
The &lt;code&gt;use&lt;/code&gt; primitive allows us to resolve the promise during the render of the component, allowing for us to use the state and props to create the promise, and then, resolving these promises and using it on our functions and JSX.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useCountTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&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;countTotalPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&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;genericPromise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countTotalPromise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&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;AsyncDerivation&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCountTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&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;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Total count is: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;result&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;At the moment I’m writing this, we don’t yet have the final release of React 19. There are some caveats and probably the primitive will evolve in the future to work in more places.&lt;/p&gt;

&lt;p&gt;One specific thing of the primitive &lt;code&gt;use&lt;/code&gt; is that it needs to be used with &lt;code&gt;Suspense&lt;/code&gt; and there is a very good reason for this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async and React components
&lt;/h2&gt;

&lt;p&gt;The conceptual idea of &lt;code&gt;await&lt;/code&gt; is nice, but it has a flaw when joined with React components. You cannot just use &lt;code&gt;await&lt;/code&gt; during the render. React calls a component to get the JSX response and use it on their flow to render a UI.&lt;/p&gt;

&lt;p&gt;If we could just stop everything in a &lt;code&gt;await&lt;/code&gt;, React could not access the children of that component and continue its work until the end of the tree. We’d stop the render flow and make the UI not be updated and freeze.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to solve that?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can look at the two examples I used in this article. The first one takes the approach of returning flags such as &lt;code&gt;loading&lt;/code&gt;, not blocking the render flow. When the promise is resolved, it throws a re-render, updating the flags, the &lt;code&gt;loading&lt;/code&gt; turns false and &lt;code&gt;data&lt;/code&gt; receives the response data.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;use&lt;/code&gt; approach is different. It really behaves like the &lt;code&gt;await&lt;/code&gt; primitive, so the component render flow is stopped there, until the resolution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wait, wait, wait, you said it was a problem, right?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And here it comes &lt;code&gt;Suspense&lt;/code&gt; for salvation. When you use the &lt;code&gt;use&lt;/code&gt; primitive, it’ll be wrapped in a &lt;code&gt;Suspense&lt;/code&gt; component, the render flow will stop waiting for the use resolution, and the user gets the &lt;code&gt;fallback&lt;/code&gt; rendered on the UI (normally a loading spinner or skeleton, something to indicate we are loading some stuff there).&lt;/p&gt;

&lt;p&gt;When the use promise is resolved, we continue the render and update the UI accordingly. No need to use &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The use primitive will be very useful for library authors aiming to use &lt;code&gt;Suspense&lt;/code&gt; and work with asynchronous behavior. For the app developer, it fixes one more case in the basic reactivity model, that will fit great in simple use cases. As it wraps promises, it is not restricted just to http requests, but for all async cases and external API usage, which can add more resources to the ecosystem.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>reactnative</category>
    </item>
    <item>
      <title>The complicated relation between React and derivation</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Tue, 29 Oct 2024 22:40:58 +0000</pubDate>
      <link>https://dev.to/felipegs/you-dont-know-derivation-on-react-55ap</link>
      <guid>https://dev.to/felipegs/you-dont-know-derivation-on-react-55ap</guid>
      <description>&lt;p&gt;React hooks was released 6 years ago, but until today, we can see errors being committed, even by senior react engineers. In the newest version of React docs, the core team put a strong effort in teaching the wrong use cases of useEffect, but errors continue happening in real projects.&lt;/p&gt;

&lt;p&gt;In this post, let’s try a different approach. Let's understand React relation with derivation and why you should use more of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reactivity
&lt;/h2&gt;

&lt;p&gt;React is not &lt;a href="https://legacy.reactjs.org/docs/design-principles.html#scheduling" rel="noopener noreferrer"&gt;fully reactive&lt;/a&gt;, but in the end, to the developer creating apps, it doesn’t matter so much. The point of difference here is that the coarse-grained approach React follows creates the necessity of re-renders to really identify what changes the state transition created.&lt;/p&gt;

&lt;p&gt;So, think in a simple React component like this one:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&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="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;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;setCount&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&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="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;text&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;When we change the count state, calling &lt;code&gt;setCount&lt;/code&gt;, we update the state value and schedule a re-render. All right, React will re-render this component calling it again. At this moment, if I ask React what changed on the render, what should be the answer?&lt;/p&gt;

&lt;p&gt;Probably a humble &lt;strong&gt;“I don’t know”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;React doesn’t track its state with a complex data structure managing its dependencies like other &lt;a href="https://www.solidjs.com/guides/reactivity" rel="noopener noreferrer"&gt;fine-grained reactive libraries do&lt;/a&gt;. React needs to call the component again. In this new call, the &lt;code&gt;count&lt;/code&gt; constant created will have the new value, and above it we will create a new constant with the string, for example &lt;strong&gt;“count is 1”&lt;/strong&gt;, if &lt;code&gt;count&lt;/code&gt; value changed to 1.&lt;/p&gt;

&lt;p&gt;Then the JSX will generate its structure with one change, the button inner text is not &lt;strong&gt;“count is 1”&lt;/strong&gt;, React does the reconciliation process, identifies this change and applies it to the real DOM. Obvious, right?&lt;/p&gt;

&lt;p&gt;At this moment, if I ask React what changed, it will probably answer: &lt;strong&gt;“The button text from Example component”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But wait, what about the text constant? It changed as well, why does it just matter the &lt;strong&gt;v-dom&lt;/strong&gt; (I know the problems with this term, but it's how it is commonly called) structure it created?&lt;/p&gt;

&lt;p&gt;For React, it doesn’t matter the variables and constants you created internally. What matters is the state changes and then, the return of the component call.&lt;/p&gt;

&lt;p&gt;Everything in the middle is part of the process of creating the view structure. Of course, all this data can affect the return JSX, and that is the point of the React model, caring about the result of the component calls and updating the DOM accordingly with the view.&lt;/p&gt;

&lt;p&gt;You probably saw the simplification of React model as:&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;view&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;f&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;View as the result of function of state. View in this case is a derivation that changes based on state. So, for this term and for the internal component data, &lt;strong&gt;React is a derivation machine&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Derivation Machine
&lt;/h2&gt;

&lt;p&gt;Every variable and constants you created inside a component will just be alive during that component call. In the example we used above, every re-render of &lt;code&gt;Example&lt;/code&gt; components created a new constant &lt;code&gt;text&lt;/code&gt;. If I need to have a new value based on some props or state, it will just create a new variable using those states and props in the calculation.&lt;/p&gt;

&lt;h3&gt;
  
  
  React creates derivations automatically, by default.
&lt;/h3&gt;

&lt;p&gt;Let’s take an example from &lt;a href="https://react.dev/learn/you-might-not-need-an-effect#updating-state-based-on-props-or-state" rel="noopener noreferrer"&gt;React docs&lt;/a&gt;:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Form&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;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFirstName&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Taylor&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLastName&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Swift&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// 🔴 Avoid: redundant state and unnecessary Effect&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;fullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFullName&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="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;useEffect&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="nf"&gt;setFullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;lastName&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="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&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;fullName&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;span&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;We have some issues here. First of all, the nature of a state.&lt;/p&gt;

&lt;p&gt;Why do we need a local state like this in an app? To keep the data and allow the user to change it. The &lt;code&gt;fullName&lt;/code&gt; state is not being changed by the user, but by the &lt;code&gt;useEffect&lt;/code&gt;. It’s using other states to create a new value. Again, in React, every variable and constant we create internally in the component can use the states and props to calculate its value: &lt;strong&gt;a derivation, the default behavior of React&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There is another problem with this example, about runtime. In this case, in the first render, the &lt;code&gt;fullName&lt;/code&gt; value will be an empty string. React will get the return of the JSX of this component, render it to the UI, follow the browser painting process and after it, call the &lt;code&gt;useEffects&lt;/code&gt; of the components. At this moment we will have the &lt;code&gt;setfullName&lt;/code&gt; call, which will schedule a new re-render. React will call the component again, now with the &lt;code&gt;fullName&lt;/code&gt; as &lt;em&gt;Taylor Swift&lt;/em&gt; and then, update the UI with the new text value.&lt;/p&gt;

&lt;p&gt;In terms of runtime, you are doing 2 renders, one of them an unnecessary one, with wrong data. It’s worse in terms of performance and stability, because the user will see the value as empty and it can be seen as a visual bug.&lt;/p&gt;

&lt;p&gt;So, following the React derivation model, we can simply change it to:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;FormRight&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;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFirstName&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Taylor&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLastName&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Swift&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;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;lastName&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&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;fullName&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;span&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;Now we have just 1 render, avoiding the unnecessary one. And we will avoid the use of the effect just using a derivation. That will be updated on every re-render using the latest version of the state values.&lt;/p&gt;

&lt;h3&gt;
  
  
  But if the calculation is too expensive? Or if I have too many states and want to update the fullName just when firstName or lastName changes?
&lt;/h3&gt;

&lt;p&gt;Right, in this case just use &lt;code&gt;useMemo&lt;/code&gt;, adding the same array of dependencies you were using on &lt;code&gt;useEffect&lt;/code&gt;. The memoization model of React is just a way to avoid the default behavior, that is to create a new derivation on every re-render. With &lt;code&gt;useMemo&lt;/code&gt;, you track manually the states and props and just create the derivation again if some of them have changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about useEffect?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; is necessary for external sync on values changes. On UI development, there are very few rare cases where this makes sense, because normally the external changes, such as server API calls, happen on user actions (we are creating &lt;strong&gt;User Interfaces&lt;/strong&gt;, by the way), so these will happen on event handlers, not in a &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you are using &lt;code&gt;useEffect&lt;/code&gt; to update a state, probably you can do the same update using derivation, avoiding all the problems mentioned before.&lt;/p&gt;

&lt;p&gt;If derivation doesn‘t work, or you have one of the few specific cases, or something is wrong with the state's design or with the solution itself. There is no problem with it, but in these cases, it’s better to review the component and avoid future problems with the component code.&lt;/p&gt;

&lt;p&gt;This is the basics about derivation in React, but we have something missing here.&lt;/p&gt;

&lt;p&gt;What happens when I need to do a derivation that is asynchronous, like a simple GET request, that uses some state as param and needs to be recalculated every time the state changes?&lt;/p&gt;

&lt;p&gt;This is a topic for the next post.&lt;br&gt;&lt;br&gt;
See you!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What was the starting point for becoming a mid-level frontend engineer?</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Mon, 06 Sep 2021 00:16:03 +0000</pubDate>
      <link>https://dev.to/felipegs/what-was-the-starting-point-to-become-a-mid-level-frontend-engineer-25gj</link>
      <guid>https://dev.to/felipegs/what-was-the-starting-point-to-become-a-mid-level-frontend-engineer-25gj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;There are some turning points in our lives that change our way of viewing things and the future. One important of mine is the podcast that I heard in mid-2014 that made me take a technical computer course and brought me to this software’s world.&lt;/p&gt;

&lt;p&gt;After that, I went to college, started to work as a junior programmer and arrived at the position that I am today, as a mid-level frontend engineer.&lt;/p&gt;

&lt;p&gt;I need to make a disclaimer here: this is not a guide or something like that, it is just a little of my experiences.&lt;/p&gt;

&lt;h2&gt;
  
  
  The initial step
&lt;/h2&gt;

&lt;p&gt;So the year was 2019, I was working at that time as a junior frontend developer to a big corporation in Brazil. That was the first time working with a big product and I had some knowledge about React, but I didn’t have experience working in real apps and the whole frontend of that company was in React.&lt;/p&gt;

&lt;p&gt;It is important to gain knowledge and practice a lot to become faster in building things and solving problems, but this is a natural process that can be accelerated if you practice more and study a lot.&lt;/p&gt;

&lt;p&gt;But when I was a junior, all the sprints had stories that scared me, I felt like I was not capable of doing that.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was that fear?
&lt;/h2&gt;

&lt;p&gt;The lack of experience was what makes everything seems harder than it actually is. This creates a fear of doing something wrong, like creating bugs on production or even in a development environment.&lt;/p&gt;

&lt;p&gt;At this point, deadlines were other terrifying stuff. Will I be able to deliver everything until the end of the sprint? Will I be able to solve this issue? Am I being a bad developer by asking help for other developers? It is normal to ask yourself this type of question, and answering them now: No, it is normal and part of the process.&lt;/p&gt;

&lt;p&gt;Another thing that I remember of that time was that I looked for the tools that I used, and as I did not really understand how that worked, they looked like magic things.&lt;/p&gt;

&lt;p&gt;At that time, I used React and Redux. Creating a reducer looked like rocket science for me. I did not understand how that was created, how that worked in reality, why I had to create a pure function and what the hell is a pure function? React looked like alien technology and so on.&lt;/p&gt;

&lt;p&gt;This causes fear too, because we fear the things that we did not understand.&lt;/p&gt;

&lt;p&gt;This point connects to the next phase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning point
&lt;/h2&gt;

&lt;p&gt;I was working, having to pay the bills and live, so I had time to study with less rush and stress. I followed the path that goes deeper in the base of frontend development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSS&lt;/strong&gt;: I did an &lt;a href="https://www.udemy.com/course/advanced-css-and-sass/"&gt;advanced course about CSS&lt;/a&gt;. That teached me how css works behind the scenes, how the box-model worked, grid, flexbox and Sass. That gives me confidence in styling applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTML&lt;/strong&gt;: I studied about semantic HTML, HTML5 and a little about accessibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt;: I read the book series “You don’t know JS”. 6 books that explain a lot about the base of the language. That changed my way to code javascript.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The more that I studied, the more I was confident to get harder tasks at work and improve my ability to solve issues and participate in meetings.&lt;/p&gt;

&lt;p&gt;But I had a real turning point that changed a lot my behavior and my vision about programming, tools and software development.&lt;/p&gt;

&lt;p&gt;It was an &lt;a href="https://indepth.dev/posts/1005/level-up-your-reverse-engineering-skills"&gt;in-depth blog’s article&lt;/a&gt; that talks about reverse-engineering, the gains of doing that and a guide of how to do that. This blog taught me a lot, they had a lot of advanced and deep articles about React, and other frameworks, like Angular.&lt;/p&gt;

&lt;p&gt;Talking more about this article, after reading it, a wall of fear broke in my head.&lt;/p&gt;

&lt;p&gt;The article explains how to learn about how a library works by reading its source code and tracking the use of the library, organizing the points of study and creating an environment to use tools like devtools to do this work.&lt;/p&gt;

&lt;p&gt;Those tools, like React and Redux, that was magic for me, were shown as just a bunch of code, with great logic, patterns and structures, of course, but in the end, it is just code.&lt;/p&gt;

&lt;p&gt;Code that I can read and understand, using data structures and design patterns that I can learn and use. After reading those articles, I started to look at source codes and learned how to build software with it. &lt;/p&gt;

&lt;p&gt;I lost that fear. &lt;/p&gt;

&lt;p&gt;And the confidence with the knowledge makes me more comfortable to use that and start to help others. At work, this makes me start to take harder tasks and carry projects alone, which, in the end, made me become &lt;br&gt;
a mid-level developer. &lt;/p&gt;

&lt;p&gt;I’m not saying that this path is simple or something like ‘read this article and you will become a mid-level developer in a few months’. This is not so simple, and getting a promotion does not even mean that you really achieve a higher level of experience and knowledge.&lt;/p&gt;

&lt;p&gt;But the whole point of this post is to recommend the in-depth article, and try to bring this point of view, that it is not something that we saw in other places.&lt;/p&gt;

&lt;p&gt;Here in dev.to and other tech publications we saw a lot of the same type of content. A huge number of beginner's guides of technologies, the same discussions and explanations of the same topic. But how many times have you seen an extensive and deep post explaining how a library or framework works behind the scenes?&lt;/p&gt;

&lt;p&gt;Look at a big codebase and understand that at the point of being able to write and explain that is not simple. It is hard to do, takes a lot of time, but can be rewarding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Having in-depth knowledge about a library can make you an expert on that and maybe become a specialist.&lt;/li&gt;
&lt;li&gt;You can contribute with that project and that can bring opportunities to you in the future. &lt;/li&gt;
&lt;li&gt;You can create advanced content about that, a type of content that we do not have in a good number.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Research source:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://indepth.dev/posts/1005/level-up-your-reverse-engineering-skills"&gt;Level Up Your Reverse Engineering Skills&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://indepth.dev/posts/1006/practical-application-of-reverse-engineering-guidelines-and-principles"&gt;Practical application of reverse-engineering guidelines and principles&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>career</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>css</category>
    </item>
    <item>
      <title>Understanding how the redux data flow really works</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Thu, 19 Aug 2021 14:58:51 +0000</pubDate>
      <link>https://dev.to/felipegs/redux-in-depth-under-the-hood-of-redux-data-flow-13he</link>
      <guid>https://dev.to/felipegs/redux-in-depth-under-the-hood-of-redux-data-flow-13he</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Redux is one of the most used state management libraries available today for web applications. Most of the developers use that, but did not know how it works behind the scenes.&lt;/p&gt;

&lt;p&gt;Some time ago I decided to read the Redux codebase, to better understand the implementation of the library that I used in some jobs. In this work, I did some notes, and this article is a more complete version of that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;: This article tries to be a deep dive into Redux. It is not a tutorial and it requires a basic knowledge about Redux, the idea here is to understand the internals and not teach how to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dataflow
&lt;/h2&gt;

&lt;p&gt;The data flow of Redux is the base of the library. It is one of the first things that we learn when we start to study Redux.&lt;/p&gt;

&lt;p&gt;You dispatch an action, that is a plain object, to the store. This updates the state using the reducer function and this new state returns to the application, updating the UI.&lt;/p&gt;

&lt;p&gt;One important thing to understand here is the architecture of Redux. It consists of the core that handles the basic features, such as dispatch actions, update the store and notify the state updates.&lt;/p&gt;

&lt;p&gt;Another part is the bindings, the most popular one that is supported by the Redux core team is the React one, called react-redux. This module connects the Redux core to react applications, creating HOC and Hooks that the react developers use to develop the UIs in the end.&lt;/p&gt;

&lt;p&gt;Our focus in this article will be the Redux core. Mainly, the store object. There is the place where the state tree is created and where it is provided the &lt;code&gt;dispatch&lt;/code&gt; and &lt;code&gt;subscribe&lt;/code&gt; methods. The both are the most important methods to Redux data flow work.&lt;/p&gt;

&lt;p&gt;To create the store, you have a function called &lt;code&gt;createStore&lt;/code&gt;. This function accepts 3 arguments: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the reducer function.&lt;/li&gt;
&lt;li&gt;the preloaded state object or most known as &lt;strong&gt;initialState&lt;/strong&gt;. This is useful for universal apps or SSR applications, because it allows the user to add a first state before the hydration process. Another use for this is when some library stores the state in local storage and reloads the state in the next section.&lt;/li&gt;
&lt;li&gt;the enhancer (this is the argument that allows the use of middlewares, and will be the theme of another article).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the creation of the store, the function does a lot of verifications to see if the reducer passed is really a function and if the preloadedState is a real object.&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;preloadedState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;enhancer&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;enhancer&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;It looks like you are passing several store enhancers to &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;createStore(). This is not supported. Instead, compose them &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;preloadedState&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;enhancer&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&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="nx"&gt;enhancer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;preloadedState&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;StoreEnhancer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Ext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StateExt&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;preloadedState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`Expected the root reducer to be a function. Instead, received: '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;kindOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;reducer&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;p&gt;Then, the function returns the store object.&lt;/p&gt;

&lt;p&gt;Internally, they create some important variables, these variables work as properties of the store object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;preloadedState&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentListeners&lt;/span&gt;&lt;span class="p"&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="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)[]&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;nextListeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentListeners&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isDispatching&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;currentReducer&lt;/strong&gt;: this variable receives the reducer function argument, that will be used to create the new state.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;currentState&lt;/strong&gt;: this variable will keep the state itself, it starts receiving the &lt;code&gt;preloadedState&lt;/code&gt;, but can be updated by other methods.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;currentListeners&lt;/strong&gt;: this variable keeps the array of listeners, that is callback functions that are executed when the state is updated. (we will dive deep into this topic later in this article).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;nextListeners&lt;/strong&gt;: this variable works as a temporary list to new listeners, to avoid some bugs when new listeners when a dispatch or notify work is in progress.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  isDispatching FLAG
&lt;/h2&gt;

&lt;p&gt;The redux library has a lot of verifications, but one appears a lot of times: this is the verification of &lt;code&gt;isDispatching&lt;/code&gt;. The idea of that is to prevent changes on the variables when the dispatch function is being called. The point is to prevent bugs with changes being made on the execution.&lt;/p&gt;

&lt;p&gt;The default value is false. The value is changed to true inside the try that updates the state. At that moment, if other methods as &lt;code&gt;getState&lt;/code&gt;, &lt;code&gt;subscribe&lt;/code&gt;, &lt;code&gt;unsubscribe&lt;/code&gt;, &lt;code&gt;dispatch&lt;/code&gt; are called, this function has verifications that throw an error, warning that these methods can not be executed correctly at that time.&lt;/p&gt;

&lt;p&gt;See an example of isDispatching verification below:&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDispatching&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You may not call store.getState() while the reducer is executing. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The reducer has already received the state as an argument. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pass it down from the top reducer instead of reading it from the store.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returning to the dataflow, it can be divided in 2 big parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dispatch&lt;/strong&gt; action and update state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notify&lt;/strong&gt; state change to subscribers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dispatch
&lt;/h2&gt;

&lt;p&gt;As shown on this basic example of Redux Documentation (&lt;a href="https://redux.js.org/introduction/getting-started#basic-example" rel="noopener noreferrer"&gt;https://redux.js.org/introduction/getting-started#basic-example&lt;/a&gt;), after using &lt;code&gt;createStore&lt;/code&gt; and having the store object available, the way to dispatch an action is to call the &lt;code&gt;dispatch&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;A&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="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isPlainObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`Actions must be plain objects. Instead, the actual type was: '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;kindOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;action&lt;/span&gt;
      &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.`&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Actions may not have an undefined "type" property. You may have misspelled an action type string constant.&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDispatching&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Reducers may not dispatch actions.&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;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isDispatching&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="nx"&gt;currentState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;currentReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;isDispatching&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&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;listeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentListeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextListeners&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;listeners&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="nf"&gt;listener&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="nx"&gt;action&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dispatch method is a simple function with only one objective, &lt;strong&gt;update the state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It receives a plain object as an argument called action. It is mandatory to have a property called &lt;code&gt;type&lt;/code&gt; on the action, this &lt;code&gt;type&lt;/code&gt; will be used on the reducer to identify the script that will create a new version of state. To make sure that an action used on the &lt;code&gt;dispatch&lt;/code&gt; call follows these rules, Redux does some verifications with the action argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isPlainObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`Actions must be plain objects. Instead, the actual type was: '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;kindOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;action&lt;/span&gt;
    &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.`&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Actions may not have an undefined "type" property. You may have misspelled an action type string constant.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Basically, it verifies if the action is a plain object, calling a util function called &lt;code&gt;isPlainObject&lt;/code&gt;. Then, it verifies if the action object has a &lt;code&gt;type&lt;/code&gt; property and if it is &lt;code&gt;undefined&lt;/code&gt;. In these cases, they throw Errors to warn the user.&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;isDispatching&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="nx"&gt;currentState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;currentReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;action&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;isDispatching&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the verifications, they do a try statement to update the state. First, they update the isDispatching flag to true (as we explained above), and then, they call the reducer function passing the last version of the state variable and the action object.&lt;/p&gt;

&lt;p&gt;The reducer will get the type of the action and based on that, will create a new version of the state. Then, they return this new state and that is assigned to the &lt;code&gt;currentState&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;This part of the code is inside a try statement, so basically, if the reducer function throws any error, this does not break the redux work. This makes the code safer on runtime. Finally, they update the &lt;code&gt;isDispatching&lt;/code&gt; to false, to maintain that work of the &lt;code&gt;isDispatching&lt;/code&gt; flag verifications.&lt;/p&gt;

&lt;p&gt;Another important point here, that explains the reason that Redux documentation says that the reducer function has to be a pure function, can be understood here. As you can see, Redux uses a simple variable to hold the state and use this variable as argument to the reducer function. &lt;/p&gt;

&lt;p&gt;As the state is an object, it is a reference pointer, so if you mutate the argument on the reducer function, you mutate the &lt;code&gt;currentState&lt;/code&gt; variable that is inside the store. And as the return of the reducer will be assigned to the &lt;code&gt;currentState&lt;/code&gt; variable, if you mutate that, will basically set to the same reference that was assigned before.&lt;/p&gt;

&lt;p&gt;It generates some issues as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Break time-travel features because all state changes, that should create different state ‘versions’, will be always the same, with the same content.&lt;/li&gt;
&lt;li&gt;Can cause bugs related with the huge number of mutations and reassign to the same reference at the same time.&lt;/li&gt;
&lt;li&gt;Can impact on changes verification, because some libraries, such as react-redux, for example, use shallow equality as the way to compare changes, as shallow equality relies on reference comparison, sometimes the state changed, but will not cause updates and re-renders.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After all this state update, they need to run the listeners to notify the subscribers that the state changed. We will talk more about this in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Notify
&lt;/h2&gt;

&lt;p&gt;The notification process of Redux is made by the method called &lt;code&gt;subscribe&lt;/code&gt;. It is basically an observer design pattern, this method allows adding a listener function that is executed after a state update.&lt;/p&gt;

&lt;p&gt;We can see the hole code of the &lt;code&gt;subscribe&lt;/code&gt; method below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&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="k"&gt;void&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;function&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;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`Expected the listener to be a function. Instead, received: '&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nf"&gt;kindOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;listener&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;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDispatching&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You may not call store.subscribe() while the reducer is executing. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;If you would like to be notified after the store has been updated, subscribe from a &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;component and invoke store.getState() in the callback to access the latest state. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;See https://redux.js.org/api/store#subscribelistener for more details.&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isSubscribed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

  &lt;span class="nf"&gt;ensureCanMutateNextListeners&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;nextListeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;unsubscribe&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isSubscribed&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDispatching&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You may not unsubscribe from a store listener while the reducer is executing. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;See https://redux.js.org/api/store#subscribelistener for more details.&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="nx"&gt;isSubscribed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

    &lt;span class="nf"&gt;ensureCanMutateNextListeners&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;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextListeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;nextListeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&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="nx"&gt;currentListeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;In the subscribe method, first, it is made 2 basic verifications, one for the &lt;code&gt;isDispatching&lt;/code&gt; and another to the listener argument, verifying if the type of the argument is really a function, to make sure that it will not break when it is called on state changes.&lt;/p&gt;

&lt;p&gt;Then, it came to the main point of this function: &lt;strong&gt;add a new listener as a subscriber&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isSubscribed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="nf"&gt;ensureCanMutateNextListeners&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;nextListeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To do that, first they create a variable called &lt;code&gt;isSubscribed&lt;/code&gt; assigning to true. The idea of this variable is to keep the internal state of that listener on the subscribers array, if it is there or not. It is important to notice that the return of the &lt;code&gt;subscribe&lt;/code&gt; function is an &lt;code&gt;unsubscribe&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So, using the concept of closure, this variable &lt;code&gt;isSubscribed&lt;/code&gt; is held in this &lt;code&gt;unsubscribe&lt;/code&gt; function. The idea is use this variable as a verification, if the listener is subscribed, the function executes the work to remove this listener from the array, if not, then do nothing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;unsubscribe&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isSubscribed&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;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Besides that, other 2 functions are executed: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One called &lt;code&gt;ensureCanMutateNextListeners&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The push of the &lt;code&gt;nextListeners&lt;/code&gt; array, that actually adds the listener to be executed in the future.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;About the &lt;code&gt;ensureCanMutateNextListeners&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ensureCanMutateNextListeners&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;nextListeners&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;currentListeners&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;nextListeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentListeners&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="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;To understand this function, we need to understand the difference between currentListeners and nextListeners.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;currentListeners&lt;/code&gt;: is the variable that keeps the listeners that are being executed or that were executed on runtime.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;nextListeners&lt;/code&gt;: is the variable that keeps the next version of listeners to be executed. This is the variable that gives the push on the subscribe function, to add a new listener. On the dispatch function, after the state update, the currentListener receives the reference of nextListeners, so if there are new listeners, they will be executed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The point of this function is that after the dispatch, the &lt;code&gt;nextListeners&lt;/code&gt; and &lt;code&gt;currentListeners&lt;/code&gt; are basically the same, pointing to the same reference. The issue is that if we just give a push to &lt;code&gt;nextListeners&lt;/code&gt;, we are affecting the &lt;code&gt;currentListeners&lt;/code&gt; variable and if a dispatch is happening at that moment, it can cause bugs.&lt;/p&gt;

&lt;p&gt;To avoid that, they created this &lt;code&gt;ensureCanMutateNextListeners&lt;/code&gt; function.The idea is just do a shallow copy of &lt;code&gt;currentListeners&lt;/code&gt;, creating a new reference. This way, if we update &lt;code&gt;nextListeners&lt;/code&gt;, we do not affect &lt;code&gt;currentListeners&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, to close the notify process, on &lt;code&gt;dispatch&lt;/code&gt; function, after the state update, all the actual listeners callbacks are called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;listeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentListeners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nextListeners&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;listeners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;listeners&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="nf"&gt;listener&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;As explained above, the currentListeners receive the nextListeners reference and this is assigned in the listeners variable. Then, they use a simple for loop to call all the listeners. This way, redux notifies all subscribers that a state update happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get state
&lt;/h2&gt;

&lt;p&gt;Imagine that a subscriber is called after a state update and wants to use the new state on the UI. How to do this? There is a function called &lt;code&gt;getState&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getState&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;S&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;isDispatching&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You may not call store.getState() while the reducer is executing. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The reducer has already received the state as an argument. &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pass it down from the top reducer instead of reading it from the store.&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="nx"&gt;currentState&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function is the simplest of the entire library. A basic verification about the &lt;code&gt;isDispatching&lt;/code&gt; is executed and after that, it is just returned the &lt;code&gt;currentState&lt;/code&gt; variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Research source:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://redux.js.org/tutorials/essentials/part-2-app-structure#reducers-and-immutable-updates" rel="noopener noreferrer"&gt;Redux Essentials Docs: Reducers and Immutable Updates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow" rel="noopener noreferrer"&gt;Redux Fundamentals Docs: Data Flow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/reduxjs/redux/blob/master/src/createStore.ts" rel="noopener noreferrer"&gt;Redux source code on master v4.1.1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>redux</category>
      <category>react</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Some thoughts about frontend learning</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Thu, 14 May 2020 02:06:12 +0000</pubDate>
      <link>https://dev.to/felipegs/some-thoughts-about-frontend-learning-21n2</link>
      <guid>https://dev.to/felipegs/some-thoughts-about-frontend-learning-21n2</guid>
      <description>&lt;p&gt;Everyday a great number of new people start to study programming and web development. The Front-end is one of the main areas of software development that are chosen by this people. And they have a lot of questions about what framework/library to learn, what tools to use and etc.&lt;/p&gt;

&lt;p&gt;But, if I could give an advice to them, I would say that the most important thing is to dominate the fundamental technologies of the web. Become advanced in this technologies, not just learn the basic stuff, because with this solid fundamentals, the path to learn advanced librarys, frameworks and tools will be easier than before.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML, CSS &amp;amp; JAVASCRIPT
&lt;/h2&gt;

&lt;p&gt;The three main Front-end technologies hide a trap. Its easy to learn the basics, but hard to learn the advanced concepts and know this advanced content will be the difference in your career as a developer.&lt;/p&gt;

&lt;p&gt;We see in great companies developers having problems making layouts with CSS, for not knowing how the box-model works; or not making the app fully accessible for not using a semantic HTML approach. And with the JavaScript this become much more important, because of the peculiarities of the language.&lt;/p&gt;

&lt;h3&gt;
  
  
  20-80 rule
&lt;/h3&gt;

&lt;p&gt;This technologies use the 20-80 rule. With 20% of the effort you will learn 80% what you need to start to work in the market. The problem is that only know the basics will make you have a lot of issues in problem solving, taking you to difficulties, bugs and errors.&lt;/p&gt;

&lt;p&gt;The big problem is that learn this other 20% requires 80% of the effort!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So the advice is to keep calm and go deep in the learning of this three technologies. Please, don’t underestimate the complexity of HTML, CSS and JavaScript. And understand that all that amazing frameworks, that huge number of names and tools is made with this three fundamental technologies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Template engines like liquid, pug, EJS and etc is converted to HTML.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The pre-processors like SASS and LESS is just CSS with some other features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React, Angular, Vue is just JavaScript with some design patterns and software engineering concepts.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The learning curve to this hype tools is very low if you have an advanced knowledge in the fundamentals. Here is an amazing website with a list of the topics that is nice to learn in the three technologies and a lot of others too: &lt;a href="https://andreasbm.github.io/web-skills/"&gt;Web Skills Website&lt;/a&gt;&lt;/p&gt;

</description>
      <category>career</category>
      <category>beginners</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding BEMCSS</title>
      <dc:creator>Felipe Gustavo</dc:creator>
      <pubDate>Sat, 16 Nov 2019 07:31:30 +0000</pubDate>
      <link>https://dev.to/felipegs/entendendo-bemcss-5dc7</link>
      <guid>https://dev.to/felipegs/entendendo-bemcss-5dc7</guid>
      <description>&lt;p&gt;When the use of CSS is no longer just the stylization of personal websites or small projects and moves to large, scalable and complex projects, there is a need to organize and think about the architecture for the CSS. Simple things like class naming can become a huge problem in future maintenance.&lt;/p&gt;

&lt;p&gt;To correct problems like these, organizational architectures and methodologies were created and one of the most popular is BEMCSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;The acronym comes from Block-Element-Modifier, which is the way designated to create names for the classes. The idea is to create a rigid standard for class naming, making it easy to read and understand what the class does and what component it targets.&lt;br&gt;
The class names follow the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.block__element--modifier&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two underlines that separate the block from the element and two lines that separate the element from the modifier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Block:
&lt;/h2&gt;

&lt;p&gt;The Block is the element or component that that class is assigned to. You don't need to respect the name of the element, the idea is that it describes what the component is in the scope of the project. For example, if you create a class for a login form, the name would not be .form, even though in HTML you do create a form.&lt;/p&gt;

&lt;p&gt;In the scope of the project the class could be *&lt;em&gt;. Login *&lt;/em&gt;, which would make it clear to everyone what it is about. A general naming rule is to separate compound names with a single dash, for example, *&lt;em&gt;. Formulario-de-cadastr *&lt;/em&gt; would be the correct way to write a name with three words&lt;/p&gt;

&lt;h2&gt;
  
  
  Element:
&lt;/h2&gt;

&lt;p&gt;The Element are internal parts of the component. In the example used above, inside the login block there would be at least two inputs for the username or email, password and an action button.&lt;/p&gt;

&lt;p&gt;The appointment of these elements would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.login__email&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.login__password&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.login__button&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Modifier:
&lt;/h2&gt;

&lt;p&gt;The modifier usually applies to changes and varied shapes for the elements or for the block itself as a whole. In the example of the article, when the user put something that does not match the password or email in the inputs and there was a check to confirm this, the visual feedback of the error, for example, could be done by changing the colors of the edges of the incorrect inputs to red.&lt;/p&gt;

&lt;p&gt;The modifier class would be used to create this, being used in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.login__email--error&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The modifiers can also be used for the block as a whole, and can be applied directly over it in the class, ignoring the existence of the elements. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.login--error&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remembering that each element has its class in an isolated way. For example, if an element has another element within it, it is not correct to join the elements into a single class, such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.bloco__elemento1__elemento2__elemento3&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In these cases each element has its own class, ignoring the order of the HTML structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.bloco&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.bloco__elemento1&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.bloco__elemento2&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="nc"&gt;.bloco__elemento3&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A new way of thinking CSS:
&lt;/h2&gt;

&lt;p&gt;When using a methodology like BEM, it is interesting to think that you are not just naming classes in a standard way. It is important to rethink how to create the elements, thinking about the layout in a modular way, separating each piece into components, which would correspond to the blocks, elements and so on.&lt;/p&gt;

&lt;p&gt;Separating the layout in this way, the creation of the code itself becomes more organized. Using BEM is just the way to formalize this new structure.&lt;br&gt;
It is important to note that BEM is a highly flexible methodology, adapting and fitting in several different CSS project architectures, another very positive point&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;The methodology is simple, but very functional and useful in the day-to-day development, in addition to being something that is often requested in job openings in the current market. Creating a scalable and quality CSS avoids rework and increases the quality of a software, therefore, it is an increasingly required skill in high-level projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Research source:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://getbem.com/"&gt;getbem&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>architecture</category>
      <category>bemcss</category>
    </item>
  </channel>
</rss>
