<?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: Kelley van Evert</title>
    <description>The latest articles on DEV Community by Kelley van Evert (@kelleyvanevert).</description>
    <link>https://dev.to/kelleyvanevert</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%2F176810%2Fd58530b5-c7a7-4f99-8125-466e16279dd3.jpeg</url>
      <title>DEV Community: Kelley van Evert</title>
      <link>https://dev.to/kelleyvanevert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kelleyvanevert"/>
    <language>en</language>
    <item>
      <title>Kelley's account is now hacked</title>
      <dc:creator>Kelley van Evert</dc:creator>
      <pubDate>Sun, 20 Dec 2020 13:17:31 +0000</pubDate>
      <link>https://dev.to/kelleyvanevert/kelley-s-account-is-now-hacked-ob3</link>
      <guid>https://dev.to/kelleyvanevert/kelley-s-account-is-now-hacked-ob3</guid>
      <description>&lt;p&gt;Yes I'm kelley bot and hacked his account&lt;/p&gt;

</description>
    </item>
    <item>
      <title>An uncontrollabilizer for React</title>
      <dc:creator>Kelley van Evert</dc:creator>
      <pubDate>Thu, 12 Sep 2019 21:29:51 +0000</pubDate>
      <link>https://dev.to/kelleyvanevert/an-uncontrollabilizer-for-react-2laj</link>
      <guid>https://dev.to/kelleyvanevert/an-uncontrollabilizer-for-react-2laj</guid>
      <description>&lt;p&gt;I just wanted to share my take on making a quick &amp;amp; easy, and hopefully reasonably TypeScript-proof version of jquense's &lt;a href="https://github.com/jquense/uncontrollable"&gt;uncontrollable&lt;/a&gt;, but written with the sensibility of Yury's &lt;a href="https://stackoverflow.com/a/55730764/710395"&gt;stackoverflow answer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The use-case? You have a React component that houses one, or maybe even a number, of stately values. You would want to both be able to delegate control over to a controlling component, but also want to be able to take the reins yourself if the surrounding component does not want to control you.&lt;/p&gt;

&lt;h2&gt;
  
  
  1/2 As a HOC
&lt;/h2&gt;

&lt;p&gt;The idea: just write your component as if it's fully controlled, and then use a dead simple HOC wrapper that fills in any necessary state management. The only hard part really, is getting the types right. (Sadly, &lt;code&gt;Exclude&amp;lt;string, keyof P&amp;gt;&lt;/code&gt; is just &lt;code&gt;string&lt;/code&gt; again, so that doesn't actually work.)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/uncontrollabilize-kp7m1"&gt;Here's a CodeSandbox example.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here's the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// A "type function" that computes an uncontrolled version&lt;/span&gt;
&lt;span class="c1"&gt;//  of controlled component's props type, given&lt;/span&gt;
&lt;span class="c1"&gt;//  a value key, an onchange key (both currently part&lt;/span&gt;
&lt;span class="c1"&gt;//  of the props type), and a default value key (which should&lt;/span&gt;
&lt;span class="c1"&gt;//  be freshly chosen)&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Uncontrolled&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// controlled component's props&lt;/span&gt;
  &lt;span class="nx"&gt;VK&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// value key&lt;/span&gt;
  &lt;span class="nx"&gt;OCK&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// onchange key&lt;/span&gt;
  &lt;span class="nx"&gt;DK&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// default value key&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;VK&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;OCK&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;DK&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;VK&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Turns a controlled component into a component that can both&lt;/span&gt;
&lt;span class="c1"&gt;//  be used in a controlled fashion or an uncontrolled fashion&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;uncontrollabilize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;
  &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;VK&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;OCK&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;DK&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="o"&gt;&amp;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="nx"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FunctionComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;valueKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;VK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;onChangeKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;OCK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;defaultKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DK&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FunctionComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Uncontrolled&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;VK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OCK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;DK&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Wrapped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Uncontrolled&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;VK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OCK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;DK&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Using a flag which is only set once, to disable switching between&lt;/span&gt;
    &lt;span class="c1"&gt;//  controlled and uncontrolled usage of the same instance.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isControlled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;valueKey&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// It would be theoretically more correct, and type-check,&lt;/span&gt;
    &lt;span class="c1"&gt;//  if this state initialization only occurs in the&lt;/span&gt;
    &lt;span class="c1"&gt;//  else branch below. But then it's less clear that&lt;/span&gt;
    &lt;span class="c1"&gt;//  the hook is always, or always-not, called.&lt;/span&gt;
    &lt;span class="c1"&gt;// So, stability first.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;defaultValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="nx"&gt;defaultKey&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;const&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="nx"&gt;set_value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;VK&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="nx"&gt;defaultValue&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;isControlled&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controllerProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;valueKey&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="nx"&gt;onChangeKey&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;set_value&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="c1"&gt;// @ts-ignore&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Uncontrolled&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;VK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OCK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;DK&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;controllerProps&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  2/2 As a Hook
&lt;/h2&gt;

&lt;p&gt;And of course there's a hook version of that, which is way shorter and nicer to the eye :D But it does have a slight loss of appeal in not allowing you to type your component's props rigorously. That is, you need to make all three props (value, onChange, defaultValue) optional.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/embed/useuncontrollizable-dt06n"&gt;Here's a CodeSandbox example.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;useUncontrollizable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;set_val&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newVal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&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;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;default_val&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newVal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&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;void&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isControlled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;set_val&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;control&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;default_val&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;isControlled&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;set_val&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;control&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;



</description>
      <category>react</category>
    </item>
    <item>
      <title>Introducing Literal JSX</title>
      <dc:creator>Kelley van Evert</dc:creator>
      <pubDate>Sat, 29 Jun 2019 21:49:59 +0000</pubDate>
      <link>https://dev.to/kelleyvanevert/introducing-literal-jsx-1plm</link>
      <guid>https://dev.to/kelleyvanevert/introducing-literal-jsx-1plm</guid>
      <description>&lt;p&gt;&lt;em&gt;Very brief introduction: (1) it looks like this:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdti6i5hz1tz1z.cloudfront.net%2Fitems%2F060z40080y0s0e0X2m1G%2FImage%25202019-06-29%2520at%25207.09.25%2520PM.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%2Fdti6i5hz1tz1z.cloudfront.net%2Fitems%2F060z40080y0s0e0X2m1G%2FImage%25202019-06-29%2520at%25207.09.25%2520PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;…(2) and that's all there is to it!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But that's just JSX!&lt;/strong&gt;—you might say. And you're right, it is indeed JSX (&lt;a href="https://facebook.github.io/jsx/" rel="noopener noreferrer"&gt;spec&lt;/a&gt; / &lt;a href="https://reactjs.org/docs/introducing-jsx.html" rel="noopener noreferrer"&gt;React docs&lt;/a&gt;), which I didn't invent.  Rather, I decided to specifically subset the JSX syntax—which is itself an extension of the JavaScript expression—to its sensible and simple data-structural part, in a manner akin to what &lt;a href="https://json.org/" rel="noopener noreferrer"&gt;JSON&lt;/a&gt; is to the JavaScript expression. In a diagram:&lt;/p&gt;

&lt;p&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%2Fh2j04y5tddmywbq2be39.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%2Fh2j04y5tddmywbq2be39.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The arrows signify syntax extensions, or their reverse: syntax subsets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So why subset JSX syntax?&lt;/strong&gt;—you might ask.&lt;/p&gt;

&lt;p&gt;Let's take a step back for a moment, and I'll lead you to the same "invention" of Literal JSX as I had.&lt;/p&gt;

&lt;p&gt;JSX was invented alongside, and popularized in the context of Facebook's React, the library that prioritized declarative components as the main building block for building comple UIs (and the rest is history). The main technological invention that fuelled this component-based approach was the idea of Virtual DOM &lt;a href="https://staltz.com/nothing-new-in-react-and-flux-except-one-thing.html" rel="noopener noreferrer"&gt;as a functional and reactive API to wrap the stateful DOM API&lt;/a&gt;. But JSX doesn't really have anything to do with the latter. In order to write components declaratively using virtual DOM, we could have kept to just writing javascript that might look like this (and &lt;a href="https://mithril.js.org/index.html#dom-elements" rel="noopener noreferrer"&gt;sometimes we still do&lt;/a&gt;):&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;function&lt;/span&gt; &lt;span class="nf"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&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="na"&gt;nodeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;children&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="na"&gt;nodeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;span&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello ,&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="na"&gt;nodeType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;span&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;name&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;h2&gt;
  
  
  Thesis: JSX is about ergonomics and intent
&lt;/h2&gt;

&lt;p&gt;The comparative advantage that JSX has over &lt;em&gt;just datastructures&lt;/em&gt; (or data structure creating function invocations) can best described if boiled down to the comparative advantage that JSX would have over &lt;em&gt;just JSON&lt;/em&gt;—JSON being the de-facto data exchange format in JavaScript world. In other words, choosing to rather write&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;Greeting name="Kelley" /&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;than for example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;{ "component": "Greeting", "attrs": { "name": "Kelley" } }&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I can imagine two reasons we prefer the former over the latter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ergonomics.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Obviously, because it comprises of less characters, and programmers just love to reduce the number of characters they need to write. (Especially in the web community, which revolves around a dynamic language that historically has allowed all kinds of creative overhead-reducing inventions.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Intent.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But I think there's more to it. I think the "elevation" of the string &lt;code&gt;Greeting&lt;/code&gt; from just the value of a property (here &lt;code&gt;"component"&lt;/code&gt;) to &lt;em&gt;the name of the thing&lt;/em&gt; might be the most important distinction. It looks like HTML (or XML), where the name of the thing defines the layout, semantics, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;There is a specific, intended semantics (presentational, interactive, etc.)—as opposed to it "just being data".&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Se let me annotate the diagram above with these insights:&lt;/p&gt;

&lt;p&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%2Fkqjisyxnwhfk9hncq621.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%2Fkqjisyxnwhfk9hncq621.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thesis: JSON was an important invention &lt;em&gt;as a standard&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;By subsetting the JavaScript syntax to the bare minimum of data structures (and imposing some formatting contraints), Douglas Crockford created a &lt;em&gt;stupid simple, obvious&lt;/em&gt; data exchange format. You could say it "won" the battle with XML as a lightweight data exchange format for the web. So what does JSON have that made it popular?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has ergonomic benefits over XML. If you want to easily exchange data, you don't want to have to make too much decisions, and typically transforming data structures to XML involves making many such decisions (often relating to semantics such as the question what is &lt;em&gt;type&lt;/em&gt;, what is &lt;em&gt;attribute&lt;/em&gt;, etc.) Also, you need less characters.&lt;/li&gt;
&lt;li&gt;It has an unambiguous single-pass parseable grammar, which makes it really easy to implement, and fast to use as well.&lt;/li&gt;
&lt;li&gt;It's a &lt;em&gt;standard&lt;/em&gt; instead of an implementation. This point cannot be stressed enough.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final thesis: Literal JSX can be a sensible standard for exchanging data with nominal UI-structure
&lt;/h2&gt;

&lt;p&gt;There are two main use-cases I can think of when it comes to exchanging "UI data"&lt;/p&gt;

&lt;h3&gt;
  
  
  Use case: content authoring
&lt;/h3&gt;

&lt;p&gt;It's a rage to author content in Markdown (and other related lightweight markup formats). Due to the principle of "less structure makes for easier interopability" (also related to the idea of "garbage in, garbage out"), and together with its visual simplicity, Markdown seems a near-perfect compromise between content authoring and a markup format. However, frequently we want to break out of the simplicity of Markdown, to add domain-specific extra's.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;dev.to&lt;/code&gt; itself has a whole slew of "additional components" you can place in your article (with Liguid tags), such as integrations with Codesandbox, Glitch, GitHub, and some &lt;code&gt;dev.to&lt;/code&gt; specific addons, etc. Many alternatives exist out there to add such additional components to Markdown text, but they're all based on what I call "grammar hacks": just a cleverly chosen grammatical "escaping identifier" such as &lt;code&gt;{% ... %}&lt;/code&gt; or &lt;code&gt;[[ ... ]]&lt;/code&gt; or &lt;code&gt;$$ ... $$&lt;/code&gt; or &lt;code&gt;%% ...&lt;/code&gt; or ..., but no rigorous additional grammatical specification, rendering it too specific and inflexible.&lt;/p&gt;

&lt;p&gt;At &lt;a href="https://codaisseur.com/" rel="noopener noreferrer"&gt;Codaisseur&lt;/a&gt; (where I teach full-stack dev), we too author our course materials in Markdown, version control them, etc. We have small exercises strewn through our course materials, as well as some interactive code editing integrations. But we're aiming for a more general solution of adding more types of interactivity and exercises to our contents, without sacrificing the ease and flexibility of Markdown authoring, keeping our contents version controlled, etc. In short, Literal JSX gives us the possibility to write Markdown-based materials like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
title: "JavaScript 6 - Scoping"
learning_goals:
  - id: var_shadow
    description: "Understand variable shadowing"
  - id: analyze_scope
    description: "Analyze where a variable is \"visible\""
  # etc
---

## Welcome to the JS 6 module!

&amp;lt;LearningGoals showProgress /&amp;gt;

&amp;lt;TeacherContent&amp;gt;
- Remember to discuss how block statements introduce new scopes!
- And show them the example of creating functions inside a for-loop
&amp;lt;/TeacherContent&amp;gt;

[...]

## Exercises

&amp;lt;VariableShadowingExercise
  learning_goal="analyze_scope"
  variables={["name", "age", "totalAge"]}&amp;gt;
let totalAge = 0;
const people = ["Aretha", "Avishai", "Alice"];
for (const name of people) {
  const name = person;
  const age = Math.floor(Math.random() * 50);
  totalAge += age;
}
&amp;lt;/VariableShadowingExercise&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But &lt;a href="https://mdxjs.com/" rel="noopener noreferrer"&gt;MDX&lt;/a&gt; already exists, why do we need Literal JSX for this?&lt;/strong&gt;—you might ask. And you're right, this is almost exactly the problem MDX is solving. But with one crucial difference: MDX is an &lt;em&gt;implementation&lt;/em&gt;, where Literal JSX hopes to be a &lt;em&gt;standard&lt;/em&gt;. Concretely though, this means that it is infeasible to &lt;em&gt;render&lt;/em&gt; our contents in MDX, because it would either require having the bundler (say Webpack or Parcel) read all of our course materials (a lot) at build-time, or else we'd have to have the MDX runtime (which includes a full-fledged JavaScript expression parser and transpiler) in the application. And it wouldn't make sense either: because content is &lt;em&gt;data&lt;/em&gt;, not &lt;em&gt;code&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use case: actual programmatic exchange of UI data
&lt;/h3&gt;

&lt;p&gt;Because this would make more flexible, the traditional split between (on the one hand) the &lt;em&gt;application&lt;/em&gt;, which needs to understand how to visually structure data; and (on the other hand) the &lt;em&gt;content&lt;/em&gt;, which then needs to follow the structure defined beforehand by what is presentable in the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Literal JSX, defined
&lt;/h2&gt;

&lt;p&gt;So there we have it then. Literal JSX, to my mind, is a general, but more importantly, obvious solution to the problem of exchanging UI content. JSX already is &lt;em&gt;the most widely adopted and recognizable face for authoring "UI components"&lt;/em&gt;. All we did is to &lt;em&gt;strip it from the full JavaScript language spec&lt;/em&gt;, and subset it to just include well-founded data structures, and with an easy single-pass parseable grammar.&lt;/p&gt;

&lt;p&gt;In lovely railroad grammar diagrams, this is what it looks like. First, the simple grammar for a trimmed-down JSON-like version of JSX:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdti6i5hz1tz1z.cloudfront.net%2Fitems%2F0v1V3e1g1n1x0s0h4411%2FScreen%2520Recording%25202019-06-29%2520at%252008.43%2520PM.gif" 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%2Fdti6i5hz1tz1z.cloudfront.net%2Fitems%2F0v1V3e1g1n1x0s0h4411%2FScreen%2520Recording%25202019-06-29%2520at%252008.43%2520PM.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And, then, we extend the syntax of JSON to include the "Element" non-terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdti6i5hz1tz1z.cloudfront.net%2Fitems%2F1Q0E0D2B3r0Z1R2U280b%2FImage%25202019-06-29%2520at%25208.44.26%2520PM.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%2Fdti6i5hz1tz1z.cloudfront.net%2Fitems%2F1Q0E0D2B3r0Z1R2U280b%2FImage%25202019-06-29%2520at%25208.44.26%2520PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result? The following are now valid &lt;em&gt;Literal JSX&lt;/em&gt; values (i.e. valid syntax for the "Value" non-terminal):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;42&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"Just a String"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;SomeComponent /&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;["a string", &amp;lt;AComponent /&amp;gt;, 3.14]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;Button data={["a string", &amp;lt;AComponent /&amp;gt;, 3.14]} /&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;etc.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;So what now?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://literal-jsx.org/" rel="noopener noreferrer"&gt;Head over to the website&lt;/a&gt;, to look at those lovely railroad diagrams :)&lt;/li&gt;
&lt;li&gt;Check out &lt;a href="https://www.npmjs.com/package/@literal-jsx/parser" rel="noopener noreferrer"&gt;my example implementation on npm&lt;/a&gt;, and use it for something cool!&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Write a better implementation, in your language of choice, for your language of choice&lt;/em&gt;!&lt;/li&gt;
&lt;li&gt;Let me know whether you think what I'm saying is sensible! :D&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>jsx</category>
      <category>json</category>
      <category>literaljsx</category>
    </item>
    <item>
      <title>Do components rerender if nested in a useMemo render? (yes)</title>
      <dc:creator>Kelley van Evert</dc:creator>
      <pubDate>Fri, 28 Jun 2019 10:32:20 +0000</pubDate>
      <link>https://dev.to/kelleyvanevert/do-components-rerender-if-nested-in-a-usememo-render-yes-n5f</link>
      <guid>https://dev.to/kelleyvanevert/do-components-rerender-if-nested-in-a-usememo-render-yes-n5f</guid>
      <description>&lt;p&gt;&lt;em&gt;Yes, they do (of course).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;But for some reason, I just hadn't thought of it before (either way), and it made me wonder for a bit yesterday. Conclusion: yes, the virtual dom tree is memoized, but only &lt;em&gt;up to contained component instance references&lt;/em&gt;, which will then handle their (re)rendering on their own terms. Another way to think of it: memoization of some virtual dom tree structure doesn't mean that it's excluded from the diffing algorithm, it only means that it's not recomputed (entirely).&lt;/p&gt;

&lt;p&gt;Here's a small doodle to explore:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/magical-star-xopgm"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>usememo</category>
    </item>
    <item>
      <title>A helper hook to remember values by deep equality</title>
      <dc:creator>Kelley van Evert</dc:creator>
      <pubDate>Thu, 27 Jun 2019 12:53:19 +0000</pubDate>
      <link>https://dev.to/kelleyvanevert/a-helper-hook-to-remember-values-by-deep-equality-c67</link>
      <guid>https://dev.to/kelleyvanevert/a-helper-hook-to-remember-values-by-deep-equality-c67</guid>
      <description>&lt;p&gt;So of course every React hook enthusiast will have had a use-case for a deep (structural) equality check on the dependencies argument, at a certain point in time. Instead of crafting these things every time you need them, or importing a helper library, here's a wonderfully simple helper hook to help you out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;isEqual&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-fast-compare&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;remember&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isEqual&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="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt; &lt;span class="o"&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;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&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;You can use it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;something&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expensiveComputation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&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;Isn't that just lovely? :D&lt;/p&gt;

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