<?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>Forem: Alex Zavalii</title>
    <description>The latest articles on Forem by Alex Zavalii (@alexandrzavalii).</description>
    <link>https://forem.com/alexandrzavalii</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%2F199538%2F9730b9c0-ee42-4b52-8121-b9f541309f6b.jpg</url>
      <title>Forem: Alex Zavalii</title>
      <link>https://forem.com/alexandrzavalii</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alexandrzavalii"/>
    <language>en</language>
    <item>
      <title>Recoil  - Ideal React State Management Library?</title>
      <dc:creator>Alex Zavalii</dc:creator>
      <pubDate>Sun, 07 Jun 2020 20:48:36 +0000</pubDate>
      <link>https://forem.com/alexandrzavalii/recoil-ideal-react-state-management-library-1203</link>
      <guid>https://forem.com/alexandrzavalii/recoil-ideal-react-state-management-library-1203</guid>
      <description>&lt;p&gt;In May 2020, Dave McCabe gave an interesting &lt;a href="https://www.youtube.com/watch?v=_ISAA_Jt9kI&amp;amp;t=448s" rel="noopener noreferrer"&gt;talk&lt;/a&gt; about the new state management library called Recoil. The video clicked immediately with me and inspired to write this article. Recoil is the missing puzzle piece for state management in React.&lt;/p&gt;

&lt;h2&gt;
  
  
  So why do we need another global state management library?
&lt;/h2&gt;

&lt;p&gt;The reason we have so many different libraries of managing state in React, is that there is no oficial one yet.&lt;/p&gt;

&lt;p&gt;Is Recoil an official one? No it is not.&lt;br&gt;
&lt;iframe class="tweet-embed" id="tweet-1262143043928502272-1" src="https://platform.twitter.com/embed/Tweet.html?id=1262143043928502272"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1262143043928502272-1');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1262143043928502272&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;But what is particularly different from the rest, is that Recoil is specifically designed for React and feels like React.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's wrong with Redux? &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Redux is by far the most popular library for managing state out there, but it does not come without drawbacks. &lt;a href="https://redux.js.org/" rel="noopener noreferrer"&gt;Redux&lt;/a&gt; same as &lt;a href="https://recoiljs.org/" rel="noopener noreferrer"&gt;Recoil&lt;/a&gt; was designed for &lt;strong&gt;performant&lt;/strong&gt;, &lt;strong&gt;predictable&lt;/strong&gt; and &lt;strong&gt;debuggable&lt;/strong&gt; updates of shared state. But let's take a look at what is not so great in Redux:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ridiculous learning curve.&lt;/strong&gt;&lt;br&gt;
Some of you might not agree with the learning curve, but to me, after I got more or less comfortable with React, I really struggled to understand Redux. Like, how to structure the project? how to store the state? where should my business logic go? Redux documentation is overwhelming, and I am sure it has all the answers to these questions, but to really nail them down, it would take you quite a bit of time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Boilerplate – reducers, actions, connectors.&lt;/strong&gt;&lt;br&gt;
Not many would disagree that Redux has too much boilerplate. If you are dealing with async data or caching computed selector values, you need to install third-party libraries solutions and add even more boilerplate. Additional code comes with the cost of having more bugs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Restructuring of Business logic&lt;/strong&gt;&lt;br&gt;
I think a Redux way of splitting business logic from the components, is not going very well with the React component based approach. Introducing Redux will require you to redesign the structure of your project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No “concurrent mode” support YET.&lt;/strong&gt;&lt;br&gt;
React &lt;a href="https://reactjs.org/docs/concurrent-mode-intro.html" rel="noopener noreferrer"&gt;concurrent mode&lt;/a&gt; is very soon to be released. "Concurrent mode" is fixing fundamental limitation by making rendering interruptible. Not supporting "concurrent mode" will be a major drawback of the library. As of now, &lt;a href="https://react-redux.js.org/" rel="noopener noreferrer"&gt;React-Redux&lt;/a&gt; does not support it, but it is planning to introduce &lt;code&gt;useMutableSource&lt;/code&gt; hook, that will make it compatible with "Concurrent Mode".&lt;br&gt;
&lt;iframe class="tweet-embed" id="tweet-1141127274487566336-769" src="https://platform.twitter.com/embed/Tweet.html?id=1141127274487566336"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1141127274487566336-769');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1141127274487566336&amp;amp;theme=dark"
  }



&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's wrong with Context API?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;Context API&lt;/a&gt; is an utility coming from React core, specifically designed for solving the &lt;a href="https://kentcdodds.com/blog/prop-drilling" rel="noopener noreferrer"&gt;prop drilling&lt;/a&gt; problem. As opposed to Redux, it is good to be used for low-frequency updates, such as theme or locale. But because of Redux drawbacks, Context API often becomes overused.&lt;br&gt;
There are some drawbacks once we start using Context as a state management tool &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Unpredictable and not performant updates.&lt;/strong&gt;&lt;br&gt;
Because of how it was designed, context lets you store only one single value per &lt;code&gt;Provider&lt;/code&gt;. If you move your global store in the value of the Provider, you will be losing out on performance. When a React &lt;code&gt;&amp;lt;Context.Provider&amp;gt;&lt;/code&gt; gets a new value, all the components that consume that value are updated and have to render, even if a component only cares about part of the state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No way of creating context dynamically.&lt;/strong&gt;&lt;br&gt;
Imagine if you have a list of items that you dynamically add or remove, and each item has to have its own context provider for isolation or for performance reasons.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F072xbe5j2407imjutcop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F072xbe5j2407imjutcop.png" alt="Dynamically inserting context"&gt;&lt;/a&gt;&lt;br&gt;
Introducing another item would require placing another Context Provider at the top of the tree, which would cause the whole tree to unmount and mount again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Context Hell&lt;/strong&gt;&lt;br&gt;
Context API definitely has much less boilerplate then redux, but if you start adding them more and more, your react tree would look something like this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgniszxk43379pu7v5mom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgniszxk43379pu7v5mom.png" alt="Context Hell"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No code split support&lt;/strong&gt;&lt;br&gt;
Because Context API introduces coupling between the roots of the tree and leaves of the tree, code splitting becomes nontrivial.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  So What is Recoil?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Very easy to learn, simple API.&lt;/strong&gt;&lt;br&gt;
To get through basic API you would literally need 5 minutes. Here is a nice free &lt;a href="https://egghead.io/playlists/up-and-running-with-recoil-a-new-state-management-library-for-react-78b8" rel="noopener noreferrer"&gt;egghead tutorial by Tomasz Łakomy&lt;/a&gt;.&lt;br&gt;
But also please check the &lt;a href="https://recoiljs.org/docs/introduction/installation" rel="noopener noreferrer"&gt;docs&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimal boilerplate and reactish approach.&lt;/strong&gt;&lt;br&gt;
Recoil does have almost zero boilerplate, and it looks and feels very like just using simple &lt;code&gt;useState&lt;/code&gt; from React. You only need to wrap your app once in &lt;code&gt;&amp;lt;RecoilRoot&amp;gt;&lt;/code&gt;, and you can have as many independent global states as you like.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performant granular updates.&lt;/strong&gt;&lt;br&gt;
Recoil allows you to connect to the exact piece of state, which is a huge performance benefit. By creating an &lt;a href="https://recoiljs.org/docs/api-reference/core/atom" rel="noopener noreferrer"&gt;atom&lt;/a&gt; you add a new global state, by creating a &lt;a href="https://recoiljs.org/docs/api-reference/core/selector" rel="noopener noreferrer"&gt;selector&lt;/a&gt; you are adding a function that returns data derived from the given set of dependency values. Both &lt;code&gt;atom&lt;/code&gt; and &lt;code&gt;selector&lt;/code&gt; can be subscribable and writable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamically created state&lt;/strong&gt;&lt;br&gt;
Recoil allows you to create dynamic global states, by using &lt;a href="https://recoiljs.org/docs/api-reference/utils/atomFamily" rel="noopener noreferrer"&gt;atomFamily&lt;/a&gt; and &lt;a href="https://recoiljs.org/docs/api-reference/utils/selectorFamily" rel="noopener noreferrer"&gt;selectorFamily&lt;/a&gt;. Both atomFamily and selectorFamily accept an id, which will dynamically create a state or a selector function.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;itemWithId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;atomFamily&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;item&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;default&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="nf"&gt;useRecoilState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;itemWithId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Async Support.&lt;/strong&gt;
Recoil comes with &lt;a href="https://recoiljs.org/docs/guides/asynchronous-data-queries#asynchronous-example" rel="noopener noreferrer"&gt;async&lt;/a&gt; support without any third-party libraries. Derived data can be synchronous or asynchronous without modifying the components that use it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchUserDetails&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;oooo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;get&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="k"&gt;try&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;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&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;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No impact on code-splitting.&lt;/strong&gt;&lt;br&gt;
Unlike Redux or ContextAPI, Recoil state is not stored globally anywhere, the state definition is incremental and distributed, which makes code-splitting achievable out of the box.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Concurrent mode support.&lt;/strong&gt;&lt;br&gt;
As opposed to other state management libraries, Recoil is built on React primitives, it is using react state under the hood. For this reason, Recoil is not usable outside of React, but it works very well within React. As of the time of writing this article, Recoil does not have the "concurrent mode" support yet but is planning to achieve it with the use of &lt;code&gt;useMutableSource&lt;/code&gt; hook in the near future.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Of course, Recoil is not ideal and there are still many unknowns. But what I like most about it, is that using Recoil feels so much familiar than existing state management libraries out there.&lt;/p&gt;

&lt;p&gt;Currently, the library is in a very early stage, but I am sure it has enough to get you excited. &lt;a href="https://recoiljs.org/docs/basic-tutorial/intro" rel="noopener noreferrer"&gt;Official Get Started&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading my thoughts on Recoil. This is my first article and I would be interested to hear your feedback.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>recoil</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
