<?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: adam klein</title>
    <description>The latest articles on DEV Community by adam klein (@adamklein).</description>
    <link>https://dev.to/adamklein</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%2F190875%2F0316c3f4-8893-411c-a6c8-81b8e913e9f5.jpeg</url>
      <title>DEV Community: adam klein</title>
      <link>https://dev.to/adamklein</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adamklein"/>
    <language>en</language>
    <item>
      <title>Coverage and courage - the pillars of your codebase</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Tue, 12 Apr 2022 18:45:03 +0000</pubDate>
      <link>https://dev.to/adamklein/coverage-and-courage-the-pillars-of-your-codebase-1gbi</link>
      <guid>https://dev.to/adamklein/coverage-and-courage-the-pillars-of-your-codebase-1gbi</guid>
      <description>&lt;p&gt;If you're going to run a marathon, you'll pace yourself and carry a bottle of water with you.&lt;/p&gt;

&lt;p&gt;Your marathon is the software you build, and we all know good software is one that you can change. Because requirements - they change all the time. Preparing for a software marathon means preparing for continuous changes of requirements, of open source libraries, of the team's structure, and ever changing best practices.&lt;/p&gt;

&lt;p&gt;But how do you prepare for change that you can't anticipate? The first ingredient is test coverage. Not just because you need to test the product before you ship a new version, but because you want the confidence to refactor your code, again and again. You want to see a nasty piece of code you didn't even intend to change, and change it along the way without thinking twice. It's like keeping your house tidy, every time you see something small out of place you put it back where it belongs. If you don't - in a few weeks you'll face long and tedious projects of making order, which you will postpone forever.&lt;/p&gt;

&lt;p&gt;The second ingredient to this formula is your courage. The courage to rewrite parts of the code even though there's a pressing deadline. The courage to try out new technologies even though it means your code will have 2 ways of doing the same thing for a while. The courage to switch to a different 3rd-party provider because you realized they're not good enough, even though you just spent 2 months integrating with them. The courage to face your non-tech colleagues and stand behind your decisions that serve the long-run, and mid-run.&lt;/p&gt;

&lt;p&gt;You have to remember that while other departments might be running sprints, your software department is running a marathon, and don't let the others set your pace for you. If you do, when you are completely out of breath and start to slow down dramatically half way through, you'll be the only one to blame for it.&lt;/p&gt;

&lt;p&gt;Sure, not every period of time in a product development lifecycle is the same, but it's your job to make sure there are enough gaps for regrouping, refactoring, and improving the infrastructure.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React hooks - debunking the myths</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Sun, 25 Apr 2021 10:31:34 +0000</pubDate>
      <link>https://dev.to/adamklein/react-hooks-debunking-the-myths-15md</link>
      <guid>https://dev.to/adamklein/react-hooks-debunking-the-myths-15md</guid>
      <description>&lt;p&gt;Sooooooooo many debates about React hooks.&lt;br&gt;
It sometimes seems like people are either in love with the feature, or they completely hate it.&lt;/p&gt;

&lt;p&gt;Myself - a hooks fan, but I'm aware of the drawbacks. I believe that everything in software is a tradeoff and has its drawbacks.&lt;/p&gt;

&lt;p&gt;On many occasions though, I find arguments against hooks that are just an afterthought and don't represent the true considerations. In my opinion at least, which I'm sharing in this post:&lt;/p&gt;

&lt;h2&gt;
  
  
  Myth #1 - hooks have these annoying rules - and that means that there's something flawed about them.
&lt;/h2&gt;

&lt;p&gt;Well, yeah - hooks have rules. But so do React classes.&lt;br&gt;
Imagine I told you that all of your state must reside on the same object, must be initialized in a single function which must be named 'constructor', and you can only define one such method. Same of side-effects, each with its own specific name. Sounds pretty restrictive, no?&lt;/p&gt;

&lt;p&gt;So it's not that hooks have rules and classes don't have, they just have different rules. &lt;/p&gt;

&lt;p&gt;In fact, these limitations of classes are not only a mental overhead, they are a true obstacle for code reuse. If you want to write reusable code that connects to a socket, updates some state, and disconnect on component unmount - it's very hard to accomplish with classes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Myth #2 - You keep encountering stale state and it's really annoying. If you need an ESLint plugin to write your code safely - there's something wrong with the feature.
&lt;/h2&gt;

&lt;p&gt;Well, if you think about it, the ESLint plugin is not a burden of hooks, it's a benefit. With classes, if you didn't re-run your effect after the dependencies changed - nothing will protect you against it. For example, if you set up a setInterval on componentDidMount but forgot to update it on componentDidUpdate, or forgot to reset it on componentWillUnmount - there is no tool that will alert you that you're doing something wrong. The &lt;code&gt;exhaustive-deps&lt;/code&gt; ESLint rule is like TypeScript for your side-effects, it prevents you from writing bugs in compile time.&lt;/p&gt;

&lt;p&gt;The same can be said about memoized values. If you memoize a function, but forget to invalidate the cache on certain values - here comes the ESLint rule to protect you against it. In fact, if you don't write any bugs, you won't even know the ESLint rule is there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Myth #3 - Hooks trade a more explicit API with a more implicit one, at the cost of moving the state and order of execution to behind the scenes
&lt;/h2&gt;

&lt;p&gt;Well, in fact that is not true. The state and order of execution are behind the scenes anyway and completely managed by React. True for hooks, and true for classes. The "this" in classes is just an illusion that makes developers feel more in control, where in fact - React is the one managing the state behind the scenes, and making sure "this.state" is updated by the time our code runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Myth #4 - You can't use all the language features with them, like loops.
&lt;/h2&gt;

&lt;p&gt;Well, have you ever tried to define a constructor in a loop? What about returning a Promise that resolves to JSX instead of returning the JSX itself - have you tried that as well?&lt;br&gt;
A framework works in a certain way and expects specific values and formats from the developer. If you don't follow the rules - things break. A sign of a good framework though, is if the rules are simple to remember and reason about and provide tooling to help prevent problems. The rules-of-hooks ESLint plugin is like an extension of TypeScript to help the developer keep the format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Side note
&lt;/h2&gt;

&lt;p&gt;That being said, I completely understand the arguments about hooks, and I'm not religious about any technology. It takes time to get used to a new syntax, and to truly understand the benefits to it.&lt;br&gt;
It's sometimes tempting to think that the people that created the API made a terrible job, and that there's probably a much better solution. But, there are many considerations that the React team has that you might have overlooked. I strongly recommend going over the discussions on Github for the Hooks RFC, and Dan Abramov's blog overreacted.io, in which he explains the motivation behine some of the decisions.&lt;/p&gt;

&lt;p&gt;And if you still disagree - that's cool :)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>package-lock.json - in GIT or not?</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Mon, 18 Jan 2021 15:48:56 +0000</pubDate>
      <link>https://dev.to/adamklein/package-lock-json-in-git-or-not-50l5</link>
      <guid>https://dev.to/adamklein/package-lock-json-in-git-or-not-50l5</guid>
      <description>&lt;h2&gt;
  
  
  tl;dr
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Put it in GIT&lt;/li&gt;
&lt;li&gt;Commit it every-time it changes&lt;/li&gt;
&lt;li&gt;Never delete it&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is version locking and why we need it?
&lt;/h2&gt;

&lt;p&gt;You write your software, commit it, push it, test it and if everything works well - deploy it to production.&lt;/p&gt;

&lt;p&gt;But how do you know that what you tested locally is the same code that is tested on the CI, and is the same code that is being deployed to production?&lt;/p&gt;

&lt;p&gt;Your code is inside GIT (or some other version control). If you pull the repository at the exact same commit, you will have the same code.&lt;/p&gt;

&lt;p&gt;But your application does not consist only of your own code. It uses external packages as well. One solution for this problem is to commit the node_modules folder to GIT, which includes all of the code your application uses. This creates a problem by itself because then every npm install will create thousands and more changes, and will make it impossible to maintain your repository.&lt;/p&gt;

&lt;p&gt;BUT, you don't really need the entire code of the packages you are using, only their versions. Every time you pull the same version from NPM repository you will get the same code.&lt;/p&gt;

&lt;p&gt;So, a lock file keeps the version of all our dependencies, and whenever someone runs &lt;code&gt;npm install&lt;/code&gt;, they will get the exact same version of your application, including external dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not just keep exact versions in package.json?
&lt;/h2&gt;

&lt;p&gt;Your package.json only points to the versions of your direct dependencies. If they have dependencies too (and they do), these versions won't be locked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not delete package-lock.json?
&lt;/h2&gt;

&lt;p&gt;Think about it, if you delete package-lock and re-install, you are forcing the latest versions of all packages in the dependency tree. Meaning, you are changing the behavior of (potentially) the entire application.&lt;/p&gt;

&lt;p&gt;What are you really trying to do? If you have some weird npm-related problem, simply remove node_modules and run &lt;code&gt;npm install&lt;/code&gt; again. Removing package-lock.json is never the solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why commit package-lock.json?
&lt;/h2&gt;

&lt;p&gt;If you don't commit it, then the version of the application everyone else will get is different than what you are running locally. This means that things might work for you, but break on the CI/production/other local machines&lt;/p&gt;

&lt;p&gt;Hope it helps to understand. And if you are still not certain about the explanation, just follow the 3 rules in the tl;dr section blindly. That's better than nothing&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React context to replace Redux?</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Thu, 06 Aug 2020 08:37:17 +0000</pubDate>
      <link>https://dev.to/adamklein/react-context-to-replace-redux-3g6e</link>
      <guid>https://dev.to/adamklein/react-context-to-replace-redux-3g6e</guid>
      <description>&lt;p&gt;React context is being used extensively for sharing state between React components. If you want proof, just go to facebook.com and open the React developer tools to see for yourself.&lt;/p&gt;

&lt;p&gt;So if Facebook is using it so much - it must be a good way to manage state, right? Well, it turns out that React Context has its drawbacks that even Facebook developers are aware of.&lt;/p&gt;

&lt;p&gt;The main 2 challenges are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Performance - unnecessary re-renders&lt;/li&gt;
&lt;li&gt;Flexibility - coupling state with the component tree&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;The first main challenge is performance. React Context doesn't allow us to use selectors and bailout of rendering. This makes it hard to reduce unnecessary renders and forces you to bend the architecture and reshape the providers all the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flexibility
&lt;/h2&gt;

&lt;p&gt;Context Providers are tied to components on the tree. This means that having dependencies between them forces you to carefully design the order of providers. Also, you cannot shape them as a tree of dependencies, since they are all on the same lineage.&lt;/p&gt;

&lt;h2&gt;
  
  
  The benefits
&lt;/h2&gt;

&lt;p&gt;The main benefits of context are that it's a built-in mechanism inside React, which means it will naturally support concurrent mode and other changes by the React team. Also, since the state is eventually just a local component state (where the provider resides) - then you can manage it using hooks as you do for the local component state.&lt;/p&gt;

&lt;h2&gt;
  
  
  Are selectors coming to Context?
&lt;/h2&gt;

&lt;p&gt;The second characteristic is built-in to Context, but there is a light at the end of the tunnel for selectors.&lt;br&gt;
There is currently an open RFC for Context selectors, and we can all hope that it gets accepted sometime soon:&lt;br&gt;
&lt;a href="https://github.com/reactjs/rfcs/pull/119"&gt;https://github.com/reactjs/rfcs/pull/119&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Found mistakes? Think otherwise? Please let me know in the comments section below.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The only state management solution that uses React hooks</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Wed, 05 Aug 2020 11:48:58 +0000</pubDate>
      <link>https://dev.to/adamklein/the-only-state-management-solution-that-uses-react-hooks-7nl</link>
      <guid>https://dev.to/adamklein/the-only-state-management-solution-that-uses-react-hooks-7nl</guid>
      <description>&lt;p&gt;As of today, we have 2 different ways of managing state and side-effects in React:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For local state and side-effects we use hooks *&lt;/li&gt;
&lt;li&gt;For global state and side-effects, we use a library such as Redux or MobX **&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;* or this.state / lifecycle methods for class components. &lt;br&gt;
** or React context, which &lt;a href="https://dev.to/adamklein/react-context-to-replace-redux-3g6e"&gt;has its drawbacks as a state management solution&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The main problem is that we are forced to manage local and global state differently. Moreover, &lt;strong&gt;we move local state to global state quite often, and the migration costs us precious development hours&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This also means that &lt;strong&gt;we don't benefit from the reusability of hooks&lt;/strong&gt;, and we can't enjoy our custom hooks in global state management.&lt;/p&gt;

&lt;h3&gt;
  
  
  ReusableJS
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;ReusableJS&lt;/strong&gt; is the only global state management solution that uses native React hooks ***&lt;br&gt;
Sure, you can use hooks to &lt;strong&gt;connect&lt;/strong&gt; to Redux or MobX stores, but inside the stores you manage state, side-effects and computed values with a different mechanism. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ReusableJS&lt;/strong&gt; allows you to manage your global state using native React hooks. It does so by wrapping each global store in a dummy component that executes the hooks, and allows you to subscribe directly to these stores, and use memoized selectors.&lt;/p&gt;

&lt;p&gt;This will allow you to manage global state the same way you manage local state, while maintaining high performance, and decoupling your state management from the component tree.&lt;/p&gt;

&lt;p&gt;To learn more about how to use ReusableJS, checkout the Repo:&lt;br&gt;
&lt;a href="https://github.com/reusablejs/reusable"&gt;https://github.com/reusablejs/reusable&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8RIoW8c1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l5azi55nv5a1hp8kaqnz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8RIoW8c1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/l5azi55nv5a1hp8kaqnz.png" alt="ReusableJS Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*** If you know of another library that allow you to manage global state using native React hooks, please let me know. I haven't encountered one after doing a pretty extensive research.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How knowledgable you are about React? See common mistakes people make</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Sun, 12 Jul 2020 11:23:50 +0000</pubDate>
      <link>https://dev.to/adamklein/how-knowledgable-you-are-about-react-see-common-mistakes-people-make-5ha4</link>
      <guid>https://dev.to/adamklein/how-knowledgable-you-are-about-react-see-common-mistakes-people-make-5ha4</guid>
      <description>&lt;h2&gt;
  
  
  Low hanging fruits
&lt;/h2&gt;

&lt;p&gt;From my experience interviewing React developers, there are things that many developers don't know, but are quite easy to learn, and will help them become better React developers, and handle quirks and bugs.&lt;/p&gt;

&lt;p&gt;They are knowledge pieces about how to use React, and how React works.&lt;/p&gt;

&lt;p&gt;Ready? Here are the the questions:&lt;/p&gt;

&lt;h2&gt;
  
  
  If we manually change the DOM, will React take this into consideration when rendering our app?
&lt;/h2&gt;

&lt;p&gt;To answer this, you need to know how the virtual DOM works.&lt;br&gt;
Plainly speaking, the virtual DOM is a representation of the DOM tree state, in plain JavaScript objects.&lt;br&gt;
When React renders our application, it renders the new virtual DOM, and compares it to the &lt;strong&gt;previous virtual DOM&lt;/strong&gt;. Notice that it doesn't look at the current state of the DOM at all in this stage.&lt;/p&gt;

&lt;p&gt;Meaning - if you manually change the DOM, React will ignore these changes, and/or override them if it re-renders that element.&lt;/p&gt;
&lt;h2&gt;
  
  
  What causes a component to re-render?
&lt;/h2&gt;

&lt;p&gt;The answer many people give is - either state changes, props change, or context changes.&lt;br&gt;
A more correct answer is - state changes, &lt;strong&gt;the parent re-renders&lt;/strong&gt;, or context changes.&lt;br&gt;
By default, React renders all child components if the parent renders, even if the previous props are exactly the same.&lt;/p&gt;

&lt;p&gt;The comparison of props only happens for pure components, or components that implement Memo/shouldComponentUpdate.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is a functional component?
&lt;/h2&gt;

&lt;p&gt;For some reason, many developers think that all functional components are pure. That is not the case. Functional components used to be stateless, but now with hooks that's not the case either. So the main difference between functional components and class components is just the syntax, and that you can only use hooks inside functional components.&lt;br&gt;
Also, there are some things you can only do with class components, like defining ErrorBoundaries.&lt;/p&gt;
&lt;h2&gt;
  
  
  Does a component name have to start with a capital letter?
&lt;/h2&gt;

&lt;p&gt;This is a tricky question. While a single variable must start with a capital letter to serve as a component, if you use a nested property then it can be a lowercase letter as well. This syntax is valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;comps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;myComp&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="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;hi&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;comps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nt"&gt;myComp&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Does state update asynchronously?
&lt;/h2&gt;

&lt;p&gt;This is a very tricky one, especially since React docs literally say that it is asynchronous.&lt;/p&gt;

&lt;p&gt;Take this code for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComp&lt;/span&gt;&lt;span class="p"&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="nx"&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;render&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;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;after setCounter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&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="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The logs will be: 'after setCounter', 'render'.&lt;br&gt;
So it seems like render does happen asynchronously after state update. But what if we add an async log after promise resolve:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComp&lt;/span&gt;&lt;span class="p"&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="nx"&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;render&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;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;promise&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;after setCounter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&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="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now the logs will be: 'after setCounter', 'render', 'promise'. Which means that render was &lt;strong&gt;synchronous&lt;/strong&gt; (it happened before the promise was resolved).&lt;/p&gt;

&lt;p&gt;So what's going on?&lt;/p&gt;

&lt;p&gt;It feels async, because React performs our event handlers inside a batch. Meaning the render function will only be invoked after the event handler finishes. All state updates are queued until then.&lt;br&gt;
So it might feel asynchronous, but it happens synchronously after the event handler finishes.&lt;/p&gt;

&lt;p&gt;To even complicate this further, in concurrent mode the render might be truly async after all.&lt;br&gt;
So React team's advice is to treat state updates as if they were always asynchronous, and I think that is good advice.&lt;/p&gt;
&lt;h2&gt;
  
  
  When to use a layoutEffect and when to use an effect?
&lt;/h2&gt;

&lt;p&gt;Layout effects receive much less attention, and many developers don't completely understand them. The main difference between them and effects, is that layout effects happen synchronously after the commit phase (meaning after the actual DOM changes), and effects happen asynchronously.&lt;/p&gt;

&lt;p&gt;So when should you use a layout effect? For example, when you want to avoid flickering of the UI. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;ScrollSync&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&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;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;useLayoutEffect&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;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;scrollTop&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;scrollTop&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;...&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We use a layout effect to bind scroll position of an element to state, to ensure it will happen synchronously and not cause any delay.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it OK to save a reference to the event parameter from event handlers?
&lt;/h2&gt;

&lt;p&gt;The answer is no.&lt;br&gt;&lt;br&gt;
The event object in React is recycled. Meaning - in the future, the same event reference might point to a different event.&lt;br&gt;&lt;br&gt;
If you must - save a reference to properties of the event, or the native event, or call event.persist() to detach it from the event pool.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap up
&lt;/h2&gt;

&lt;p&gt;I hope you learned something new, or at least got some order or new perspectives about the things you already know.&lt;/p&gt;

&lt;p&gt;If you have any feedback or I made a mistake - please let me know in the comments section.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>We don't know how React state hook works</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Wed, 08 Jul 2020 06:46:58 +0000</pubDate>
      <link>https://dev.to/adamklein/we-don-t-know-how-react-state-hook-works-1lp8</link>
      <guid>https://dev.to/adamklein/we-don-t-know-how-react-state-hook-works-1lp8</guid>
      <description>&lt;p&gt;This article is about:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When is the state updated&lt;/li&gt;
&lt;li&gt;The update queue and lazy computation&lt;/li&gt;
&lt;li&gt;Batching&lt;/li&gt;
&lt;li&gt;useState vs. useReducer&lt;/li&gt;
&lt;li&gt;Performance optimizations

&lt;ul&gt;
&lt;li&gt;eagerly computing state updates&lt;/li&gt;
&lt;li&gt;shallow rendering and bailing out&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Will the updater function always run?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When is the state updated?
&lt;/h2&gt;

&lt;p&gt;Look at this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="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="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What would you imagine happen after the button is clicked and setCounter is called? Is it this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React calls the updater function (prev =&amp;gt; prev + 1)&lt;/li&gt;
&lt;li&gt;Updates the hook's state (= 1)&lt;/li&gt;
&lt;li&gt;Re-renders component&lt;/li&gt;
&lt;li&gt;Render function calls useState and gets updated state (== 1)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this is what you imagine - then you are wrong. I was also wrong about this, until I did some experiments and looked inside the hooks source code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The update queue and lazy computation
&lt;/h2&gt;

&lt;p&gt;It turns out, every hook has an update queue. When you call the &lt;code&gt;setState&lt;/code&gt; function, React doesn't call the updater function immediately, but saves it inside the queue, and schedules a re-render.&lt;/p&gt;

&lt;p&gt;There might be more updates after this one, to this hook, other hooks, or even hooks in other components in the tree.&lt;br&gt;
There might be a Redux action that causes updates in many different places in the tree. All of these updates are queued - nothing is computed yet.&lt;/p&gt;

&lt;p&gt;Finally, React re-renders all components that were scheduled to be rendered, top-down. But the state updates are still not performed.&lt;/p&gt;

&lt;p&gt;It's only when useState actually runs, during the render function, that React runs each action in the queue, updates the final state, and returns it back.&lt;/p&gt;

&lt;p&gt;This is called &lt;code&gt;lazy computation&lt;/code&gt; - React will calculate the new state only when it actually needs it.&lt;/p&gt;

&lt;p&gt;To summarize, what happens is this (simplified):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React queue's an action (our updater function) for this hook&lt;/li&gt;
&lt;li&gt;Schedules a re-render to the component&lt;/li&gt;
&lt;li&gt;When render actually runs (more about this later):

&lt;ul&gt;
&lt;li&gt;Render runs the useState call&lt;/li&gt;
&lt;li&gt;Only then, during useState, React goes over the update queue and invokes each action, and saves the final result in the hook's state (in our case - it will be 1)&lt;/li&gt;
&lt;li&gt;useState returns 1&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Batching
&lt;/h2&gt;

&lt;p&gt;So when does React say: "OK, enough queueing updates and scheduling renders, let me do my job now"? How does it know we're done updating?&lt;/p&gt;

&lt;p&gt;Whenever there's an event handler (onClick, onKeyPress, etc.) React runs the provided callback inside a batch.&lt;br&gt;
The batch is synchronous, it runs the callback, and then flushes all the renders that were scheduled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// batch starts&lt;/span&gt;
    &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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="c1"&gt;// schedule render&lt;/span&gt;
    &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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="c1"&gt;// schedule render&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// only here the render will run&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What if you have any async code inside the callback? That will be run outside the batch. In this case, React will &lt;em&gt;immediately&lt;/em&gt; start the render phase, and not schedule it for later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt; &lt;span class="c1"&gt;// batch already finished&lt;/span&gt;
    &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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="c1"&gt;// render immediately&lt;/span&gt;
    &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&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="c1"&gt;// render immediately&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Click me&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  State is Reducer
&lt;/h2&gt;

&lt;p&gt;I mentioned earlier that "React runs each action in the queue". Who said anything about an action?&lt;/p&gt;

&lt;p&gt;It turns out, under the hood, &lt;code&gt;useState&lt;/code&gt; is simply &lt;code&gt;useReducer&lt;/code&gt; with the following &lt;code&gt;basicStateReducer&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;basicStateReducer&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;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;return&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;action&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="nx"&gt;action&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="nx"&gt;action&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;So, our &lt;code&gt;setCounter&lt;/code&gt; function is actually &lt;code&gt;dispatch&lt;/code&gt;, and whatever you send to it (a value or an updater function) is the action.&lt;/p&gt;

&lt;p&gt;Everything that we said about &lt;code&gt;useState&lt;/code&gt; is valid for &lt;code&gt;useReducer&lt;/code&gt;, since they both use the same mechanism beind the scenes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance optimizations
&lt;/h2&gt;

&lt;p&gt;You might think - if React computes the new state during render time, how can it bail out of render if the state didn't change? It's a chicken and egg problem.&lt;/p&gt;

&lt;p&gt;There are 2 parts to this answer.&lt;/p&gt;

&lt;p&gt;There's actually another step to the process. In some cases, when React knows that it can avoid re-render, it will eagerly compute the action. This means that it &lt;em&gt;will&lt;/em&gt; run it immediately, check if the result is different than the previous state, and if it's equal - it will not schedule a re-render.&lt;/p&gt;

&lt;p&gt;The second scenario, is when React is not able to eagerly invoke the action, but during render React figures out nothing changed, and all state hooks returned the same result. The React team explains this best inside their docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Bailing out of a state update&lt;/strong&gt;&lt;br&gt;
If you update a State Hook to the same value as the current state, React will bail out without rendering the children or firing effects. (React uses the Object.is comparison algorithm.)&lt;/p&gt;

&lt;p&gt;Note that React may still need to render that specific component again before bailing out. That shouldn’t be a concern because React won’t unnecessarily go “deeper” into the tree. If you’re doing expensive calculations while rendering, you can optimize them with useMemo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update"&gt;https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Put shortly, React may run the render function and stop there if nothing changed, and won't really re-render the component and its children.&lt;/p&gt;

&lt;h2&gt;
  
  
  Will the updater function always run?
&lt;/h2&gt;

&lt;p&gt;The answer is no. For example, if there's any exception that will prevent the render function from running, or stop it in the middle, we won't get to the &lt;code&gt;useState&lt;/code&gt; call, and won't run the update queue.&lt;/p&gt;

&lt;p&gt;Another option, is that during the next render phase our component is unmounted (for example if some flag changed inside the parent component). Meaning the render function won't even run, let alone the &lt;code&gt;useState&lt;/code&gt; expression.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learned something new? Found any mistakes?
&lt;/h2&gt;

&lt;p&gt;Let me know in the comments section below&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why Code Reviews Shouldn't Exist</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Wed, 20 May 2020 10:30:51 +0000</pubDate>
      <link>https://dev.to/adamklein/why-code-reviews-shouldn-t-exist-3noa</link>
      <guid>https://dev.to/adamklein/why-code-reviews-shouldn-t-exist-3noa</guid>
      <description>&lt;p&gt;CRs (Code Reviews) are the industry standard for keeping our code quality and sharing best practices between team members.&lt;br&gt;
But if you think about what a CR is - it's someone reviewing your work &lt;strong&gt;after&lt;/strong&gt; you're already done with it, and made a context-switch to a different task, and a different branch. Not a very effective way to ensure code quality.&lt;/p&gt;

&lt;h1&gt;
  
  
  Automation
&lt;/h1&gt;

&lt;p&gt;Our coding practices evolved during the years. We tackle this problem by creating small commits, ensuring PRs (Pull Requests) are not getting stale and moving a large portion of code quality control to automated tools.&lt;/p&gt;

&lt;p&gt;Code styling remarks are a thing of the past. We have linters and prettifiers, which means no more endless debates about code styling. Almost all errors are caught during coding time, and some are automatically fixed.&lt;/p&gt;

&lt;p&gt;Most of these tasks already have automated tools that solve them.&lt;br&gt;
Automated tests help verify the correctness of the code and regression.&lt;br&gt;
We have automated checks that verify your code meets the performance metrics.&lt;/p&gt;

&lt;p&gt;3rd party tools based on AI are starting to review our PRs automatically, and even create pull requests themselves, keeping industry standards, making patches to close security vulnerabilities, and updating open source packages.&lt;/p&gt;

&lt;p&gt;What about code design and architecture?&lt;br&gt;
If you think about it, if you've already written your code, wrote tests, and created a pull request - now it is probably a little too late for someone to suggest that you re-design your code.&lt;br&gt;
Enter design reviews - ask for feedback &lt;strong&gt;before&lt;/strong&gt; you write the code, and not after it.&lt;/p&gt;

&lt;h1&gt;
  
  
  The future of CRs - continuous review
&lt;/h1&gt;

&lt;p&gt;Despite the current automation, we still do CRs. That's because we always want another pair of eyes on the code, and automation is still basic and can't understand the context of our code. So how will CRs change in the future?&lt;/p&gt;

&lt;p&gt;In the future, more and more automation and collaboration will influence our coding environment. Thanks to AI, we will have tools that alert us in real-time if our code is duplicate or inefficient and if it conflicts with some other change that is being written by another developer. They will also suggest better names for our variables and methods and keep them consistent throughout the system.&lt;/p&gt;

&lt;p&gt;The timespan between writing code and having it reviewed - by human and machine - will get shorter and shorter until it becomes continuous. The tools that shape our environment will become smarter, more real-time, and more collaborative.&lt;/p&gt;

&lt;p&gt;The tools that allow us to collaborate will become more efficient and user friendly and will have a better and more intimate understanding of our work. These tools could, for example, allow us to review the history of comments and discussions around our code, who are the main contributors to it, ask for a review in real-time, and help us schedule the reviews to avoid bottlenecks and outdated branches.&lt;/p&gt;

&lt;p&gt;So, unless AI can write code instead of us, and put us all out of work, I anticipate a bright future for developer productivity and happiness, and a future which lets us focus even more on what we like doing best - coding.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>productivity</category>
      <category>architecture</category>
    </item>
    <item>
      <title>The true code behind the explosive buzzword marketing messages</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Fri, 01 May 2020 07:11:15 +0000</pubDate>
      <link>https://dev.to/adamklein/the-true-code-behind-the-explosive-buzzword-marketing-messages-1mhm</link>
      <guid>https://dev.to/adamklein/the-true-code-behind-the-explosive-buzzword-marketing-messages-1mhm</guid>
      <description>&lt;h2&gt;
  
  
  AI driven personalization and individualization
&lt;/h2&gt;



&lt;div class="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;isReturningUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Welcome back!&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Welcome!&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;h2&gt;
  
  
  Cutting-edge security algorithms
&lt;/h2&gt;



&lt;div class="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;hashedPassword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// TODO: read about salt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  AI-based text sentiment analysis
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&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="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;phrase&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;angry&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="s1"&gt;upset&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="s1"&gt;pissed off&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="s1"&gt;disappointed&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="s1"&gt;absurd&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="s1"&gt;manager&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="s1"&gt;get me your manager&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="s1"&gt;get me the manager&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="s1"&gt;I want to speak with the manager&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="s1"&gt;your manager&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="s1"&gt;the manager&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="s1"&gt;a manager&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="s1"&gt;shut up!&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="s1"&gt;!!!&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="s1"&gt;insane&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="s1"&gt;crazy&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="s1"&gt;stop arguing with me&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="s1"&gt;would you just listen?!&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="s1"&gt;how many times&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="s1"&gt;no!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;F_WORD&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; you!`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;F_WORD&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; off!`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;F_WORD&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;phrase&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;SENTIMENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ANGRY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;SENTIMENTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NEUTRAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// TODO: We are not using SENTIMENTS.HAPPY - consider removing&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Rendering engine
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&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;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;render&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  State of the art rendering engine
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;requestAnimationFrame&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;components&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&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;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;render&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="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Analytics
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sendAnalytics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sendToServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/analytics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onclick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sendAnalytics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Actionable Analytics
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sendActionableAnalytics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sendToServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/analytics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onclick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sendActionableAnalytics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clicked&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Real-time collaboration
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;setInterval&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.server.com/newData&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNewData&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;isNewData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;refreshEverything&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Data driven business intelligence
&lt;/h2&gt;



&lt;div class="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;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastLoggedIn&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;MONTHS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;sendSmartAlert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;SMART_ALERTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;USER_CHURN_ALERT&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;h4&gt;
  
  
  Contribute more examples using the comments
&lt;/h4&gt;

&lt;p&gt;If you think of funnier examples - comment and I'll include them (if they're good)&lt;/p&gt;

</description>
    </item>
    <item>
      <title>5 ways that will change how you write React hooks</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Sat, 25 Apr 2020 19:22:26 +0000</pubDate>
      <link>https://dev.to/adamklein/5-ways-that-will-change-how-you-write-react-hooks-1h71</link>
      <guid>https://dev.to/adamklein/5-ways-that-will-change-how-you-write-react-hooks-1h71</guid>
      <description>&lt;p&gt;NOTE!&lt;br&gt;&lt;br&gt;
Tips and methods in this post are my own personal preference, and I'm sure many people will disagree.&lt;br&gt;&lt;br&gt;
There is no right or wrong. Every approach has its pros and cons. If you take just one good thing out of the post - I've done my share.&lt;br&gt;&lt;br&gt;
I do not mean to insult anyone who thinks differently. Some of the "bad code" examples used to be code that I've written myself!&lt;br&gt;&lt;br&gt;
If you do think differently - you are more then welcome to comment and change my mind.  &lt;/p&gt;

&lt;p&gt;Good reading!&lt;/p&gt;
&lt;h2&gt;
  
  
  1. An effect has no name
&lt;/h2&gt;

&lt;p&gt;Writing several effects in the same component?&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&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="p"&gt;...&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&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="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;I don't want to read your code just to know what they are doing... duh...&lt;/p&gt;

&lt;p&gt;Here's a tip for you, use named functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&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;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;subscribeToUpdates&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;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;useInterval&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="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Much better right?&lt;br&gt;&lt;br&gt;
There's another benefit - you will see the effect name in React dev tools:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VzfXyI19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sed4i1lvfjt4ahabfgn7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VzfXyI19--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sed4i1lvfjt4ahabfgn7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't be a smarty pants and try to extract to constants, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&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="nx"&gt;subscribeToUpdates&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="nx"&gt;useInterval&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subscribeToUpdates&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;useInterval&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;Because then you are only fooling the linter, not me!&lt;br&gt;
(Exhaustive deps rules won't work for the function implementations)&lt;/p&gt;
&lt;h2&gt;
  
  
  2. async functions
&lt;/h2&gt;

&lt;p&gt;Effects don't support async functions (you can't return a promise).&lt;br&gt;
It's so annoying, let's try to solve it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&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="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&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;WTF?! IIFE?! Are we in 2010?!&lt;br&gt;
Try again please:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
  &lt;span class="nx"&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;fetchData&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;No! You are not listening! (See comment above about exhaustive deps)&lt;/p&gt;

&lt;p&gt;OK, I'll give it to you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomethingAsync&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;doSomethingAsync&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;Sometimes you just gotta be verbose with them code.  &lt;/p&gt;

&lt;p&gt;Or, if you insist on taking the function out, take it out completely from the component and pass it the deps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomethingAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dep1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dep2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;doSomethingAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dep1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dep2&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;dep1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dep2&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;h2&gt;
  
  
  3. Debouncing the hooks way
&lt;/h2&gt;

&lt;p&gt;It's really stupid to implement your own debounce when there is a ton of libraries out there who already have. Right?!&lt;/p&gt;

&lt;p&gt;Wrong! 'Cause now we got hooks!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;MyComp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomethingDebounced&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;timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;doSomethingWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&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="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeout&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;value&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;Ummm.... what?&lt;br&gt;
Yes, that is an implementation of debounce with nothing but effect, timeout and cleanup function. You're smart, think about it.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. useCallbacks? Nahhh....
&lt;/h2&gt;

&lt;p&gt;You might think that &lt;code&gt;useReducer&lt;/code&gt; is better than &lt;code&gt;useState&lt;/code&gt; when managing a complex object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reducer&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;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;switch&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;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MOVE_RIGHT&lt;/span&gt;&lt;span class="dl"&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;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;left&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;left&lt;/span&gt; &lt;span class="o"&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;step&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MOVE_DOWN&lt;/span&gt;&lt;span class="dl"&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;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;top&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;top&lt;/span&gt; &lt;span class="o"&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;step&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&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;state&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useReducer&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;top&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But think about it, you will still have to use &lt;code&gt;useCallback&lt;/code&gt; for each action dispatch if you want a stable ref:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;moveRight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;step&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;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MOVE_RIGHT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;step&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;Think about this version instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPosition&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;top&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;actions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;moveRight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;step&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;setPosition&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;left&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;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;moveDown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;step&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;setPosition&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;top&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;top&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All actions are memoized!&lt;br&gt;
And no switch case, meaning better debugging experience and TypeScript integration.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Use &lt;code&gt;useContext&lt;/code&gt; with selectors to bail out of render
&lt;/h2&gt;

&lt;p&gt;You've probably heard many times that it's impossible to bail out of rendering if you're using Context.&lt;/p&gt;

&lt;p&gt;Well.....&lt;br&gt;&lt;br&gt;
You're right. I'll give it to you.&lt;/p&gt;

&lt;p&gt;But cross your fingers, 'cause selectors are coming to a version near you:&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/reactjs/rfcs/pull/119"&gt;https://github.com/reactjs/rfcs/pull/119&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When this is accepted, we would be able to do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;someProperty&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Hopefully in the next few months, but who knows, right?!&lt;/p&gt;

&lt;h3&gt;
  
  
  Hope you learned something new! Tell your friends.
&lt;/h3&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>hooks</category>
    </item>
    <item>
      <title>You don't need useReducer</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Wed, 13 Nov 2019 09:15:36 +0000</pubDate>
      <link>https://dev.to/adamklein/you-don-t-need-usereducer-ok0</link>
      <guid>https://dev.to/adamklein/you-don-t-need-usereducer-ok0</guid>
      <description>&lt;p&gt;There's a misconception these days in the React community that whenever you want to manage a complex object instead of breaking down to individual state variables, you should use &lt;code&gt;useReducer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, as I'll show in this article, managing a complex object and the changes it can go through can be more easily done using &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Show me the code
&lt;/h2&gt;

&lt;p&gt;OK, here are the 2 versions, which are pretty much equivalent in result:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useReducer:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reducer&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;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;switch&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;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MOVE_RIGHT&lt;/span&gt;&lt;span class="dl"&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;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;left&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;left&lt;/span&gt; &lt;span class="o"&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;step&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MOVE_DOWN&lt;/span&gt;&lt;span class="dl"&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;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;top&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;top&lt;/span&gt; &lt;span class="o"&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;step&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nl"&gt;default&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;state&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useReducer&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="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MOVE_RIGHT&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;step&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&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;useState:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&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;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPosition&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;top&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;actions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&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="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;moveRight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;step&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;setPosition&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;left&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;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;moveDown&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;step&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;setPosition&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;top&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;top&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;step&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;actions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;moveRight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note the &lt;code&gt;useMemo&lt;/code&gt;. The reason it is there is to make sure we gain feature parity with &lt;code&gt;useReducer&lt;/code&gt;. Since the &lt;code&gt;dispatch&lt;/code&gt; parameter of &lt;code&gt;useReducer&lt;/code&gt; is always constant throughout renders, I wanted the actions to be that as well&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  So what did we gain?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Easier TypeScript support.&lt;br&gt;
Inside the reducer, you are dealing with different types of actions, each with its own parameters. To get that working well with TypeScript you'll need to add a type per action and follow some rules to make sure TypeScript can differentiate between the different action types inside the switch case.&lt;br&gt;
It's less intuitive and more verbose than working with plain function signatures, which have the parameter types colocated next to the implementation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Better IDE Support&lt;br&gt;
If you use functions and not action objects, you can jump to its definition, look for references to a function, and rename it globally with the help of the IDE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Less error-prone&lt;br&gt;
String action types are prone to undetected errors. You can obviously bypass this with some constants or TypeScript magic, but that means you need to add more boilerplate code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Did we lose anything?
&lt;/h2&gt;

&lt;p&gt;It's easier to test a reducer. Or is it?&lt;br&gt;
Since a reducer is a pure function, it's easier to test. Testing a function that uses the setter function will require some extra wiring.&lt;/p&gt;

&lt;p&gt;But, we could easily write a test-util once that will help us test object actions, and reuse it where-ever we need. Moreover, the benefits of functions over action objects will also prove useful inside tests, such as better TS and IDE support.&lt;/p&gt;

&lt;p&gt;So all in all, I would argue that the benefits surpass the drawbacks in terms of testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What about reducers in Redux?
&lt;/h2&gt;

&lt;p&gt;For a global state manager, there are other benefits to using action objects. Several reducers can handle the same action, you get a nice view of the history of the app state using the devtools, and it's easy to export and import entire user flows. You can think of every state mutation as a user intent that is expressed using an object - and it opens up more possibilities.&lt;/p&gt;

&lt;p&gt;But for a local state, these benefits do not exist. You always handle the action in the same reducer, and the action history of a single component is not that interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Change my mind
&lt;/h2&gt;

&lt;p&gt;I know it's a very common pattern in the industry to use a &lt;code&gt;useReducer&lt;/code&gt;, but I really hate doing things just because they're popular if they don't make any sense to me.&lt;br&gt;
So I'm very very very much open to other opinions and ideas, and if you can change my mind I'd love to see how.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>A recipe for deciding between technologies</title>
      <dc:creator>adam klein</dc:creator>
      <pubDate>Sat, 14 Sep 2019 16:08:36 +0000</pubDate>
      <link>https://dev.to/adamklein/a-recipe-for-deciding-between-technologies-40a1</link>
      <guid>https://dev.to/adamklein/a-recipe-for-deciding-between-technologies-40a1</guid>
      <description>&lt;p&gt;I've written in the past why I think choosing between technologies is extremely difficult.&lt;/p&gt;

&lt;p&gt;So, here are my 2 cents on how to do that effectively. It's not a bullet-proof method, but I hope it will help guide you on that journey:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do some unstructured and unbiased research of pure facts and verify them against different sources. Don’t make any prejudgments and don’t choose sides.&lt;/li&gt;
&lt;li&gt;Look at who’s writing the article and what education they have on the subject. Try to understand if it’s a technical, experienced person, or a content writer. Check out the comments as well.&lt;/li&gt;
&lt;li&gt;Find people that you trust and that face similar challenges, and share your view on development, and consult with them.&lt;/li&gt;
&lt;li&gt;Write down a list of features for each technology.&lt;/li&gt;
&lt;li&gt;Find the features that satisfy your needs in all options, and remove them from the list. For example, if all technologies are performant enough for you — no need to compare their performance.&lt;/li&gt;
&lt;li&gt;Decide on each feature what is a pro and what is a con for you.&lt;/li&gt;
&lt;li&gt;Think about the things that matter the most to you. For each technology, write down at most 3–4 pros/cons that are important for you, and ignore the rest.&lt;/li&gt;
&lt;li&gt;Go over the list and see if there's any technology that you can already cross out based on the pros/cons list.&lt;/li&gt;
&lt;li&gt;For the remaining techs, make a PoC, and determine the barrier of entry and the quality of the documentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Your choice
&lt;/h2&gt;

&lt;p&gt;Remember, these are &lt;strong&gt;YOUR&lt;/strong&gt; personal interpretations. Some people will see a framework’s full solution as a pro, some will see it as a con. Some don’t really care about popularity, for others, it’s the most important requirement.&lt;/p&gt;

&lt;p&gt;Your choice will affect future developers for potentially years, so making a good research at this stage is worthwhile.&lt;/p&gt;

&lt;p&gt;Good luck!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>architecture</category>
      <category>technologies</category>
      <category>techlead</category>
    </item>
  </channel>
</rss>
