{"componentChunkName":"component---src-templates-tag-js","path":"/tags/recoil/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":1,"edges":[{"node":{"fields":{"slug":"/engineering/recoiling-the-react/"},"html":"<p><a href=\"https://recoiljs.org/\" title=\"Recoil js\">Recoil js</a> is another state management library for React.</p>\n<p>Though we already have Redux. Mobx, Context, but here we got a new light entry in the community.</p>\n<p>Why I am saying <code>light</code> you will figure it out at the end of this blog.</p>\n<h2 id=\"why-recoil\" style=\"position:relative;\"><a href=\"#why-recoil\" aria-label=\"why recoil permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why Recoil</h2>\n<ul>\n<li>Firstly, it solves the global state management problems.</li>\n<li>Easy to learn; there are no new major principles n logic to learn.</li>\n<li>Quite Simple, it is similar like react.</li>\n<li>Though I like redux, but it will not take that much time to learn.</li>\n</ul>\n<h2 id=\"recoil--concepts\" style=\"position:relative;\"><a href=\"#recoil--concepts\" aria-label=\"recoil  concepts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Recoil  concepts</h2>\n<p>There are two major concepts:</p>\n<ol>\n<li><strong>Atoms</strong> </li>\n<li><strong>Selectors</strong></li>\n</ol>\n<p>Something new? Don't worry will understand this quickly.</p>\n<h3 id=\"atoms\" style=\"position:relative;\"><a href=\"#atoms\" aria-label=\"atoms permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Atoms</h3>\n<p> This is similar to how we use the <code>useState</code> hook in react. If you are new to react hooks, checkout <a href=\"https://www.loginradius.com/blog/engineering/react-hooks-guide/\" title=\"react hooks guide\">react hooks guide</a>.</p>\n<p> e.g.:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"> const [age, setAge] = useState(0);</span></code></pre>\n<p><strong>age</strong> : this will be used for state variable declaration.\n<strong>setAge</strong> :this will be used for state variable updations.</p>\n<h4 id=\"1-lets-start-with-binding-root-app-with-recoil\" style=\"position:relative;\"><a href=\"#1-lets-start-with-binding-root-app-with-recoil\" aria-label=\"1 lets start with binding root app with recoil permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Let's start with binding root app with Recoil</h4>\n<p>Now you need to make a few alterations in your app's root level, like the below snippet.</p>\n<p><code>RecoilRoot</code> will behave like a global context provider that will share the global state to your app tree.</p>\n<p>Now update your older code snippet.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">ReactDOM.render( &lt;AppPage /&gt;, document.getElementById(&quot;root&quot;))</span></code></pre>\n<p>to </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import { RecoilRoot } from &quot;recoil&quot;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">ReactDOM.render( </span>\n<span class=\"grvsc-line\"> &lt;RecoilRoot&gt;</span>\n<span class=\"grvsc-line\">    &lt;AppPage /&gt;</span>\n<span class=\"grvsc-line\"> &lt;/RecoilRoot&gt;, document.getElementById(&quot;root&quot;))</span></code></pre>\n<h4 id=\"2-set-the-atom\" style=\"position:relative;\"><a href=\"#2-set-the-atom\" aria-label=\"2 set the atom permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Set the Atom</h4>\n<p>Recoil calls this part as <strong>atom</strong>, where we set the global key, value.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import { atom } from &quot;recoil&quot;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const age = atom({</span>\n<span class=\"grvsc-line\">  key: &quot;age&quot;, </span>\n<span class=\"grvsc-line\">  default: 0  // default global value for age key</span>\n<span class=\"grvsc-line\">});</span></code></pre>\n<p>Now this atom will be accessible throughout the app.</p>\n<h4 id=\"3-use-the-atom\" style=\"position:relative;\"><a href=\"#3-use-the-atom\" aria-label=\"3 use the atom permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. use the atom</h4>\n<p>Now update your older code snippet.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"> const [age, setAge] = useState(0);</span></code></pre>\n<p>to </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import { useRecoilState } from &#39;recoil&#39;</span>\n<span class=\"grvsc-line\">const [ageState, setAge] = useRecoilState(age);</span></code></pre>\n<p>For a Larger view, here is <strong>Full Code</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import { atom,useRecoilState } from &#39;recoil&#39;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const age = atom({</span>\n<span class=\"grvsc-line\">  key: &quot;age&quot;, </span>\n<span class=\"grvsc-line\">  default: 0  // default global value for age key</span>\n<span class=\"grvsc-line\">});</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export const AgeCalculator = () =&gt; {</span>\n<span class=\"grvsc-line\">const [ageState, setAge] = useRecoilState(age);</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>Quite easy. Isn't it?\nNow let's move to another part <strong>selectors</strong>.</p>\n<h3 id=\"selectors\" style=\"position:relative;\"><a href=\"#selectors\" aria-label=\"selectors permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Selectors</h3>\n<p>This is similar to how Redux manages states.</p>\n<p>if a value can be derived/computed from the state, then we can skip re initialize of an another separate state key, for that use selectors.</p>\n<h4 id=\"1-set-the-selectors\" style=\"position:relative;\"><a href=\"#1-set-the-selectors\" aria-label=\"1 set the selectors permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. set the selectors</h4>\n<p>In the below example, we are getting <code>isChild</code> value from <code>age</code> key, then we can directly use <strong>selector</strong> instead of adding a new key or <strong>atom</strong>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import {selector} from &#39;recoil&#39;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const isChild = selector({</span>\n<span class=\"grvsc-line\">  key: &quot;childage&quot;,</span>\n<span class=\"grvsc-line\">  get: ({ get }) =&gt; {</span>\n<span class=\"grvsc-line\">    const state = get(age);</span>\n<span class=\"grvsc-line\">    return state &lt; 10 ;</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">});</span></code></pre>\n<h4 id=\"2-use-the-selectors\" style=\"position:relative;\"><a href=\"#2-use-the-selectors\" aria-label=\"2 use the selectors permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. use the selectors</h4>\n<p>Now will use the above-mentioned selector.</p>\n<p>below code snippet use <code>age</code> value to <code>isChild</code>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">  const [ageState, setAge] = useRecoilState(age);</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">  const value = useRecoilValue(isChild);</span></code></pre>\n<p>For a larger view, here is <strong>Full Code</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">import {selector, useRecoilState} from &#39;recoil&#39; </span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">const isChild = selector({</span>\n<span class=\"grvsc-line\">  key: &quot;childage&quot;,</span>\n<span class=\"grvsc-line\">  get: ({ get }) =&gt; {</span>\n<span class=\"grvsc-line\">    const state = get(age);</span>\n<span class=\"grvsc-line\">    return state &lt; 10 ;</span>\n<span class=\"grvsc-line\">  }</span>\n<span class=\"grvsc-line\">});</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">export const AgeCalculator = () =&gt; {</span>\n<span class=\"grvsc-line\">    const [ageState, setAge] = useRecoilState(age);</span>\n<span class=\"grvsc-line\">   const value = useRecoilValue(isChild);</span>\n<span class=\"grvsc-line\">}</span></code></pre>\n<p>That's all, Milord.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>I hope you got why I am saying this as <strong><code>light</code></strong>.</p>\n<p>Recoil is looking relatively easy, promising, and early-stage, but again this is the next generation, and it will take time to get the own space in the <a href=\"https://community.loginradius.com/\">community</a>.  </p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"April 30, 2021","updated_date":null,"title":"What is recoil.js and how it is managing in react?","tags":["JavaScript","React","Recoil"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/f37a15f3f8cda8461c5455bdd2895e0c/ee604/react.png","srcSet":"/static/f37a15f3f8cda8461c5455bdd2895e0c/69585/react.png 200w,\n/static/f37a15f3f8cda8461c5455bdd2895e0c/497c6/react.png 400w,\n/static/f37a15f3f8cda8461c5455bdd2895e0c/ee604/react.png 800w,\n/static/f37a15f3f8cda8461c5455bdd2895e0c/f3583/react.png 1200w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Abhimanyu Singh Rathore","github":"abhir9","avatar":null}}}}]}},"pageContext":{"tag":"Recoil"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}