<?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: Jens Böttcher</title>
    <description>The latest articles on DEV Community by Jens Böttcher (@eljenso).</description>
    <link>https://dev.to/eljenso</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%2F186668%2F56942d9d-547e-4259-8039-ff19a883ca0f.png</url>
      <title>DEV Community: Jens Böttcher</title>
      <link>https://dev.to/eljenso</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eljenso"/>
    <language>en</language>
    <item>
      <title>Mounting React components in a different framework</title>
      <dc:creator>Jens Böttcher</dc:creator>
      <pubDate>Wed, 26 Jun 2019 19:38:53 +0000</pubDate>
      <link>https://dev.to/eljenso/react-component-mounting-in-a-different-framework-10mm</link>
      <guid>https://dev.to/eljenso/react-component-mounting-in-a-different-framework-10mm</guid>
      <description>&lt;p&gt;The project we are working on started as a &lt;a href="https://backbonejs.org/"&gt;Backbone.js&lt;/a&gt; project, but we now began to integrate React into it.&lt;br&gt;
This post is not about the reasoning behind that, but about something different:&lt;br&gt;
how we use (or mount) React components inside a Backbone application.&lt;/p&gt;

&lt;p&gt;When we write a new React app from scratch, we define our entrypoint component, usually called &lt;code&gt;App&lt;/code&gt;, and mount it somewhere via ReactDOM into the existing DOM:&lt;br&gt;
&lt;code&gt;ReactDOM.render(&amp;lt;App /&amp;gt;, document.getElementById("root"));&lt;/code&gt;.&lt;br&gt;
We will then start to develop the application, which completely resides in that &lt;code&gt;App&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;But this is not the case when we have an existing application written with another framework (in our case backbone), that we now want to use React inside it.&lt;br&gt;
Our choices were to either:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rewrite the whole application from scratch&lt;/li&gt;
&lt;li&gt;Realize new features with React, and slowly replace Backbone.js code by React code in the process&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For many reasons (which might be discussed in a future post), we chose option 2.&lt;/p&gt;



&lt;p&gt;Lets define a new component that we want to integrate in our existing application:&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;CounterButton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Define state using hooks&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Return button displaying current state and incrementing state on click&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;CounterButton&lt;/code&gt; component renders a button that shows how often the user has clicked on it.&lt;br&gt;
This component has a state &lt;code&gt;count&lt;/code&gt;, initially set to &lt;code&gt;0&lt;/code&gt;, and the corresponding setter function &lt;code&gt;setCount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, in order to add &lt;code&gt;CounterButton&lt;/code&gt; to our existing application at some place, we use &lt;code&gt;ReactDOM.render&lt;/code&gt; to render it into an existing DOM element:&lt;br&gt;
&lt;code&gt;ReactDOM.render(&amp;lt;CounterButton /&amp;gt;, document.getElementById("someElement"));&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And we are done!&lt;/p&gt;



&lt;p&gt;&lt;em&gt;Or so we thought.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What if you want to reuse the same component at the same place at a later time?&lt;br&gt;
For example a modal (also known as dialogue), that the user closes at some point but might eventually open up again.&lt;/p&gt;

&lt;p&gt;Let's add a &lt;code&gt;show&lt;/code&gt; state to the &lt;code&gt;CounterButton&lt;/code&gt; component, which can make the &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; disappear:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;CounterButton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Define state using hooks&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setShow&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;useState&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;span class="c1"&gt;// Return button displaying current state and incrementing state on click&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;show&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="kc"&gt;null&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&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="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;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;setShow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CounterButton&lt;/code&gt; will now return &lt;code&gt;null&lt;/code&gt; if &lt;code&gt;!show&lt;/code&gt; yields true, completely removing &lt;code&gt;&amp;lt;button&amp;gt;&lt;/code&gt; from the DOM when that &lt;code&gt;show&lt;/code&gt; state changes from &lt;code&gt;true&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
Here, this is the case when &lt;code&gt;count&lt;/code&gt; is &lt;code&gt;5&lt;/code&gt; at the time the user clicks the button.&lt;/p&gt;

&lt;p&gt;This logic is what we currently use to close a modal.&lt;br&gt;
When the user triggers the close logic of that modal, we set the &lt;code&gt;show&lt;/code&gt; state to &lt;code&gt;false&lt;/code&gt; which result in the modal being removed from the DOM..&lt;/p&gt;

&lt;p&gt;But what if you want to show &lt;code&gt;CounterButton&lt;/code&gt; again after it disappeared?&lt;br&gt;
Simply execute the following call again, right?&lt;br&gt;
&lt;code&gt;ReactDOM.render(&amp;lt;CounterButton /&amp;gt;, document.getElementById("someElement"));&lt;/code&gt;&lt;br&gt;
Sadly, &lt;code&gt;CounterButton&lt;/code&gt; will not show up.&lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://reactjs.org/docs/react-dom.html#render"&gt;React docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the React element was previously rendered into container, this will perform an update on it and only mutate the DOM as necessary to reflect the latest React element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, ReactDOM will render the &lt;em&gt;same instance&lt;/em&gt; as before, only with updated props.&lt;br&gt;
React will use the instance of &lt;code&gt;CounterButton&lt;/code&gt;, that was previously used, with the same state: &lt;code&gt;show&lt;/code&gt; is still &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our first idea to solve this issue was to create a new instance of &lt;code&gt;CounterButton&lt;/code&gt; every time before we pass it to &lt;code&gt;ReactDOM.render&lt;/code&gt;.&lt;br&gt;
For this, we encapsulated the body of the &lt;code&gt;CounterButton&lt;/code&gt; function inside an arrow function, essentially an anonymous functional component. &lt;code&gt;CounterButton&lt;/code&gt; will now return this anonymous functional component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;CounterButton&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Define state using hooks&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;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&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;show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setShow&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;useState&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;span class="c1"&gt;// Return button displaying current state and incrementing state on click&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;show&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="kc"&gt;null&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&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="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;count&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;setShow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&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="c1"&gt;// Create new functional component to pass into ReactDOM.render&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CounterButtonInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;CounterButton&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CounterButtonInstance&lt;/span&gt;  &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;No matter how often we call &lt;code&gt;ReactDOM.render&lt;/code&gt; with a return of &lt;code&gt;CounterButton()&lt;/code&gt; into &lt;code&gt;document.getElementById("root")&lt;/code&gt;, &lt;code&gt;ReactDOM.render&lt;/code&gt; will always see this anonymous functional component as different component as the one before.&lt;br&gt;
That is because it &lt;em&gt;is&lt;/em&gt; a different anonymous functional component.&lt;/p&gt;

&lt;p&gt;But this approach has at least one issue:&lt;br&gt;
&lt;code&gt;CounterButton&lt;/code&gt; is not a functional component anymore, but a function returning a functional component.&lt;br&gt;
This makes reusing &lt;code&gt;CounterButton&lt;/code&gt; inside a React application impossible.&lt;/p&gt;

&lt;p&gt;Now, for our current solution, we removed that encapsulation introduced in the last code snippet.&lt;br&gt;
Instead, we make use of the special component prop &lt;code&gt;key&lt;/code&gt;, read more about it the &lt;a href="https://reactjs.org/docs/lists-and-keys.html"&gt;React docs&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="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;CounterButton&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;getTime&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;, document.getElementById&lt;/span&gt;&lt;span class="se"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;"root"&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We make use of an important attribute of the &lt;code&gt;key&lt;/code&gt; prop here: if React is about to re-render a component which has its &lt;code&gt;key&lt;/code&gt; &lt;em&gt;changed&lt;/em&gt; since the last render, React will discard that previous version and render it from scratch. &lt;br&gt;
We use the current time (in milliseconds) as value for that prop; and since this will change between renders, React will create a new instance of &lt;code&gt;CounterButton&lt;/code&gt; with a fresh state! 🎉&lt;/p&gt;

&lt;p&gt;Below you see a codepen showcasing this approach.&lt;br&gt;
Click that button a few times, and it will disappear to never come back again.&lt;br&gt;
But if you uncomment those key props, &lt;code&gt;CounterButton&lt;/code&gt; will get reset every 2 seconds.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/eljenso/embed/WqZzQq?height=600&amp;amp;default-tab=js,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Some afterthoughts&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For that anonymous functional component, we could also had introduced another function that returns an anonymous functional returning the original &lt;code&gt;CounterButton&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;CreateCounterButton&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;CounterButton&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;Calling &lt;code&gt;CreateCounterButton&lt;/code&gt; will then create a new instance of &lt;code&gt;CounterButton&lt;/code&gt; on every call.&lt;br&gt;
This will keep our &lt;code&gt;CounterButton&lt;/code&gt; reusable.&lt;/p&gt;

&lt;p&gt;Any of the approaches described above have a drawback:&lt;br&gt;
&lt;code&gt;CounterButton&lt;/code&gt; will still be part of the ReactDOM, even after its removed from the DOM.&lt;br&gt;
We should make sure that &lt;code&gt;CounterButton&lt;/code&gt; is properly unmounted from the ReactDOM once it is not used anymore; otherwise, it can be considered a memory leak, which can result in performance issues.&lt;br&gt;
ReactDOM provides an &lt;code&gt;unmountComponentAtNode(container)&lt;/code&gt; method, which allows to unmount any React component mounted in the &lt;code&gt;container&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our example, we would utilize it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unmountComponentAtNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But since &lt;code&gt;CounterButton&lt;/code&gt; is not, and should not be, aware that it has to be unmounted this way, that call should be handled from the outside.&lt;/p&gt;

&lt;p&gt;We did not look further into using &lt;code&gt;unmountComponentAtNode&lt;/code&gt; yet.&lt;br&gt;
Since we do not have many React components yet (we currently have around 40 tsx files in the codebase), the &lt;code&gt;key&lt;/code&gt; prop approach seems sufficient.&lt;br&gt;
We should look further into this approach once the think that leaving unused components in the ReactDOM affects the performance of our application.&lt;/p&gt;

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