<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jehan Kulathilaka on Medium]]></title>
        <description><![CDATA[Stories by Jehan Kulathilaka on Medium]]></description>
        <link>https://medium.com/@jehan98?source=rss-3953b430d92d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zMaiiE44XnjWGEUXQf9Q1Q.jpeg</url>
            <title>Stories by Jehan Kulathilaka on Medium</title>
            <link>https://medium.com/@jehan98?source=rss-3953b430d92d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 20:26:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jehan98/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use Zustand?]]></title>
            <link>https://medium.com/@jehan98/how-to-use-zustand-80a501a0578e?source=rss-3953b430d92d------2</link>
            <guid isPermaLink="false">https://medium.com/p/80a501a0578e</guid>
            <dc:creator><![CDATA[Jehan Kulathilaka]]></dc:creator>
            <pubDate>Mon, 19 Sep 2022 03:16:17 GMT</pubDate>
            <atom:updated>2022-09-22T18:33:36.944Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4ej-1rHTfJ5ji7_5XscrWg.jpeg" /><figcaption>Zustand</figcaption></figure><p>Before jumping into the example, let’s understand what it is about.</p><p><strong>What is it about?</strong></p><p>Zustand is a state management tool for react. It uses react hooks as its primary pattern on interacting with the state. One key point is zustand comes with significantly less boilerplate compared to other state management tools out there.</p><p><strong>How to use Zustand?</strong></p><p>Let’s work through a small example to get the idea. By the way, hope you are a <strong><em>Matrix Fan</em></strong>. <strong>:)</strong></p><p>Read the text in the following figure to understand the logic of the app we are going to build. This will be the interface of the app we are building.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*1zA7Bwqlyxot-aHy3k_HDg.png" /><figcaption>Matrix</figcaption></figure><p>Since you are trying to understand a state management tool, Hope you have already installed node. npm is already available after you finished installing node. But if you want to work with yarn you have to install it beforehand. Run the following code in the terminal to install it globally.</p><pre>npm install --global yarn</pre><p><strong>Step 1: Setting up React development environment</strong></p><p>Following code sets up a development environment so that you can use the latest JavaScript features.</p><p>This will take some time. Wait until fully installed.</p><pre>npx create-react-app neo</pre><p><strong>Step 2: Install zustand using npm or yarn</strong></p><p>Run the following code to install zustand to the project.</p><pre>npm install zustand<br>yarn add zustand</pre><p><strong>Step 3: Creating a store</strong></p><p>Under the “src” directory create another directory as “state”. Inside this create a JavaScript file as “data.js”.</p><p>Open this file and as the first step let’s import from zustand.</p><pre>import create from ‘zustand’</pre><p>Now as we have imported “create”, let’s use this to create a store. To create the store copy &amp; paste the following code.</p><pre>const useDataStore = create((set) =&gt; ({<br>  distance: 1,<br>  alive: true,<br>  run: () =&gt; {<br>    set((state) =&gt; ({distance: state.distance + 1}));<br>  },<br>  lookback: () =&gt; {<br>    set((state) =&gt; ({<br>      distance: state.distance — 2,<br>      alive: state.distance &gt;= 2 ? true : false,<br>    }));<br>  },<br>}));</pre><p>Here we have created two states as “distance” and “alive”. We have also created two functions to manipulate the states as well.</p><p><strong><em>run() function</em></strong></p><p>As you may have already understood, the run function increases the distance between you (Neo) and the Agent.</p><p><strong><em>lookBack() function</em></strong></p><p>Well, you are Neo, right? Then, you are smart. You don’t have to run if Agent Jones stops chasing right? So, you have to look back to see whether he is still chasing you or not. But the situation is Agent is never stopping. Therefore, every time you look back you get closer to him by twice the rate. Until you can keep a distance from the Agent you can dodge any bullet and stay alive.</p><p><strong>Step 4: Using the store</strong></p><p>Now you have created a store. Next step is to use the store to create states.</p><pre>export function useGetData() {<br>  const {distance, alive} = useDataStore();<br>  const run = useDataStore((state) =&gt; state.run);<br>  const lookBack = useDataStore((state) =&gt; state.lookBack);<br>  <br>  return {<br>    distance,<br>    alive,<br>    run,<br>    lookBack,<br>  };<br>}</pre><p>Now your “data.js” file should be same as this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2898588c36a03040af9048122b0935d5/href">https://medium.com/media/2898588c36a03040af9048122b0935d5/href</a></iframe><p><strong>Step 5: Include Shooter Image</strong></p><p>Choose any image you prefer and rename it as “Shooter.png”. Then include it in the “assets” directory inside the “src” directory.</p><p>Now let’s move in to the “App.js” file.</p><p><strong>Step 6: Using the store</strong></p><p>First, let’s import Shooter image and states.</p><pre>import ShooterImg from &quot;./assets/Shooter.png&quot;;<br>import { useGetData } from &quot;./state/data&quot;;</pre><p>To extract each state and function use the following code inside the App() function.</p><pre>const {alive, distance, run, lookBack} = useGetData();</pre><p>Then copy &amp; paste following code inside the return().</p><pre>&lt;div className=&quot;App&quot;&gt;<br>  &lt;img src={ShooterImg} alt=&quot;logo&quot; width=&quot;300&quot;/&gt;<br>  &lt;p&gt;<br>    &lt;strong&gt;Hello Neo. &lt;/strong&gt;<br>    You are inside the Matrix<br>    &lt;br /&gt;<br>  &lt;/p&gt;<br>  &lt;p&gt;<br>    There is a &lt;strong&gt;shooter&lt;/strong&gt;. It&#39;s &lt;strong&gt;Agent Jones.      &lt;/strong&gt;<br>    &lt;br /&gt;<br>    You immediately start running away. Now you are bit away from him and can   do two things.<br>    &lt;br /&gt;<br>    You can run further, or you can look back.<br>    &lt;br /&gt;<br>    As long as you are keeping distance you can dodge any bullet {&quot; &quot;}<br>    &lt;strong&gt;:)&lt;/strong&gt;<br>    &lt;br /&gt;<br>    &lt;strong&gt;distance: {distance}&lt;/strong&gt;<br>    &lt;/p&gt;<br>    &lt;button disabled={!alive} onClick={run}&gt;<br>    Run<br>    &lt;/button&gt;<br>  &lt;button disabled={!alive} onClick={lookBack}&gt;<br>    look Back<br>  &lt;/button&gt;<br>  &lt;br /&gt;<br>  {!alive ? &quot;Died :(&quot; : &quot;Still Alive :)&quot;}<br>&lt;/div&gt;</pre><p>Now your App.js file should be same as this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e246ac6459b2d62604779ca505bd1d5/href">https://medium.com/media/3e246ac6459b2d62604779ca505bd1d5/href</a></iframe><p>That’s just it. You can start using zustand right away. Since now you know how to use zustand, you may be wondering why use zustand with added complexity when we can easily use useState() here. But the problems arise when the components have so many levels and you have to pass states inside components which increases complexity and reduces the readability of the code drastically.</p><p>There is so much about zustand than this. You will learn them along the way. Good Luck.</p><p><strong>References</strong></p><p>[1]: <a href="https://github.com/pmndrs/zustand/blob/main/bear.jpg"><em>zustand/bear.jpg at main · pmndrs/zustand (github.com)</em></a></p><p>[2]: <a href="https://www.bing.com/images/search?view=detailV2&amp;ccid=%2fm8T03kD&amp;id=64CEF1EF41EDCE6E8DA689CDBB31DB5EA9849893&amp;thid=OIP._m8T03kDWa-YFpY-2CrCHgHaLj&amp;mediaurl=https%3a%2f%2fth.bing.com%2fth%2fid%2fR.fe6f13d3790359af9816963ed82ac21e%3frik%3dk5iEqV7bMbvNiQ%26riu%3dhttp%253a%252f%252fvignette1.wikia.nocookie.net%252fmatrix%252fimages%252f4%252f4d%252fAgent-smith-the-matrix-movie-hd-wallpaper-2880x1800-4710.png%252frevision%252flatest%253fcb%253d20140504013834%26ehk%3du%252b6P9hzkf4%252bY2ARKt87hhAS4G7bjhF9sTcAtPVLyiC4%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&amp;exph=1398&amp;expw=896&amp;q=agent+jones+matrix&amp;simid=608045607267209258&amp;FORM=IRPRST&amp;ck=296447B1976F7C0F87D32D3DEEEDF84B&amp;selectedIndex=32&amp;ajaxhist=0&amp;ajaxserp=0"><em>agent jones matrix — Bing images</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80a501a0578e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>