<?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: Anand</title>
    <description>The latest articles on DEV Community by Anand (@sutikshna).</description>
    <link>https://dev.to/sutikshna</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%2F2962%2FoXJJe7n1.jpg</url>
      <title>DEV Community: Anand</title>
      <link>https://dev.to/sutikshna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sutikshna"/>
    <language>en</language>
    <item>
      <title>State Chart in Real-world</title>
      <dc:creator>Anand</dc:creator>
      <pubDate>Sat, 06 Feb 2021 00:44:37 +0000</pubDate>
      <link>https://dev.to/sutikshna/state-chart-in-real-world-1hn5</link>
      <guid>https://dev.to/sutikshna/state-chart-in-real-world-1hn5</guid>
      <description>&lt;h2&gt;
  
  
  Implementing Complex Requirement in React
&lt;/h2&gt;

&lt;p&gt;Due to complex business requirements, the moment a React developer adds multiple &lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useState&lt;/code&gt; &amp;amp; &lt;code&gt;useQuery&lt;/code&gt;, he/she finds that such component's code is not fun to read &amp;amp; change.&lt;/p&gt;

&lt;p&gt;I worked on similar business requirement for our  Login component :-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On load of Login page, fetch product offerings, and wait till it finishes. &lt;/li&gt;
&lt;li&gt;Take the user to the error page if it fails.&lt;/li&gt;
&lt;li&gt;Verify that &lt;code&gt;product_code&lt;/code&gt; query parameter present in URL belongs to one of the products in product list recently loaded, if not then redirect to product offering page.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;product_code&lt;/code&gt; query parameter is valid, then verify if accessToken present in sessionStorage/cookie is valid and not expired, if yes redirect user to retrieve application flow.&lt;/li&gt;
&lt;li&gt;if accessToken is not valid, load the latest marketing campaign. Wait till it get loaded and then redirect user to OAuth based login flow (we show latest campaign on login page).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I implemented the above using plain &lt;code&gt;useEffect&lt;/code&gt;, &lt;code&gt;useQuery&lt;/code&gt; of apollo and &lt;code&gt;useState&lt;/code&gt;, but code was complex. I spent a couple of hours making it possible to comprehend, but wasn't satisfied with the end result. Most of you surely have similar business requirements. I am curious to find how you solve such a requirement using React or any other way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Statechart based development
&lt;/h2&gt;

&lt;p&gt;For a simple example app, it may look over-engineering to use state-machine (though &lt;a href="https://www.youtube.com/watch?v=VU1NKX6Qkxc"&gt;conf talks by David&lt;/a&gt; explains how even, simple requirement hide complexities/bugs in it). &lt;br&gt;
We use &lt;a href="https://xstate.js.org/"&gt;xstate&lt;/a&gt; to implement a long funnel in our application.&lt;br&gt;
I was aware about state/transitions/events/guards in xstate. We used it to build the skeleton of our funnel and flow in pages. But we were yet to utilize -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://xstate.js.org/docs/guides/actions.html"&gt;actions&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://xstate.js.org/docs/guides/communication.html"&gt;services&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xstate.js.org/docs/guides/transitions.html#eventless-always-transitions"&gt;Eventless Transitions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two concepts are what make it really useful to tackle non-trivial real world problems using state-charts. This is how my machine looked for above requirement -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RO1iceei--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdsgrmafdh86ybreqvvj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RO1iceei--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdsgrmafdh86ybreqvvj.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then I realise that state-chart spec (or XState docs) is vast because it does cover all our use cases. We still have a long way to adopt xstate fully, but I am sure it'd be worth since it offers following lofty benefits :-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Formalism of logic and &lt;a href="https://xstate.js.org/viz/"&gt;Visualization&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Ease of unit testing (compare to unit testing the logic written in react UI components) &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=tpNmPKjPSFQ&amp;amp;t"&gt;Auto Generated E2E test code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Ease of &lt;a href="https://www.youtube.com/watch?v=RqTxtOXcv8Y&amp;amp;t"&gt;applying Analytics&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am already seeing the first two benefits in our codebase, As a team, we are trying to reach the level where we can gain the benefit of (3. &amp;amp; 4.).&lt;/p&gt;

</description>
      <category>statechart</category>
      <category>xstate</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
