<?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: Aleksandr Anokhin</title>
    <description>The latest articles on DEV Community by Aleksandr Anokhin (@sanohin).</description>
    <link>https://dev.to/sanohin</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%2F105084%2Fbe2d53b0-4f65-43ab-9423-2bc1c69720e1.jpeg</url>
      <title>DEV Community: Aleksandr Anokhin</title>
      <link>https://dev.to/sanohin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sanohin"/>
    <language>en</language>
    <item>
      <title>Managing UI state in location query</title>
      <dc:creator>Aleksandr Anokhin</dc:creator>
      <pubDate>Fri, 25 Jan 2019 21:03:41 +0000</pubDate>
      <link>https://dev.to/sanohin/managing-ui-state-in-location-query-1cad</link>
      <guid>https://dev.to/sanohin/managing-ui-state-in-location-query-1cad</guid>
      <description>&lt;p&gt;I think every frontend developer had to store somewhere value of open dialog, selected tab etc.&lt;br&gt;
For example, you have a simple requirement to open some small settings modal when a user clicks a button:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faau09ny9p3ulg1rbgi79.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Faau09ny9p3ulg1rbgi79.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you use React, then you are lucky, you've got component state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;openDialog&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;dialogOpen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But then designer wants this dialog to open from few random components like here:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxbux1hw8onpayqmqpb9j.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fxbux1hw8onpayqmqpb9j.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These two dropdowns are different components with different state and in one of them, we have our &lt;code&gt;SettingsDialog&lt;/code&gt; component. So what should we do now?&lt;br&gt;
Add &lt;code&gt;SettingsDialog&lt;/code&gt; to the second dropdown, same openDialog state key, and handlers? Or we can go further and put handlers and state key to a HOC to reuse this functionality? If we use redux we can also create UI reducer or use it if we have. UI reducer is just a messy object with different key-value pairs that are used and changed from across the whole application in random places. Here is the most primitive implementation (however complete enough):&lt;br&gt;
&lt;/p&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;ui&lt;/span&gt; &lt;span class="o"&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="nx"&gt;action&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="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="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ui/set&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;{&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="nx"&gt;key&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;value&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;state&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;Though, we can improve this. Not everyone uses redux even. We can keep the state of open dialog in the URL like "&lt;a href="http://my.app?openDialog=true" rel="noopener noreferrer"&gt;http://my.app?openDialog=true&lt;/a&gt;".&lt;br&gt;
If a user refreshes the page, he still would see the dialog open. It is especially essential when we keep the state of selected tab like:&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwn84nj12ecnbc20yv1x8.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fwn84nj12ecnbc20yv1x8.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the best use case is probably of storing state in query is table filters and sort column. As a result, user would be able to share the same state he has.&lt;/p&gt;

&lt;p&gt;So be flexible, keep state not only in your React components and redux.&lt;/p&gt;

</description>
      <category>react</category>
      <category>state</category>
      <category>query</category>
      <category>redux</category>
    </item>
  </channel>
</rss>
