<?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>Forem: Carsten Daurehøj</title>
    <description>The latest articles on Forem by Carsten Daurehøj (@arelstone).</description>
    <link>https://forem.com/arelstone</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%2F107691%2Fd99923f9-27e8-46f5-833d-3261e1172186.jpeg</url>
      <title>Forem: Carsten Daurehøj</title>
      <link>https://forem.com/arelstone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arelstone"/>
    <language>en</language>
    <item>
      <title>Seeing the light with React.Context - Multiple React.Context in a class component</title>
      <dc:creator>Carsten Daurehøj</dc:creator>
      <pubDate>Mon, 21 Jun 2021 12:35:30 +0000</pubDate>
      <link>https://forem.com/arelstone/seeing-the-light-with-react-context-in-classes-multiple-react-context-in-class-a-component-447d</link>
      <guid>https://forem.com/arelstone/seeing-the-light-with-react-context-in-classes-multiple-react-context-in-class-a-component-447d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;A question I quickly had to ask ask my self was: "How do you have multiple contexts in a React class component?" - useContext to the rescue&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've been working as a software engineer for +10 years where I basically only have been doing CRUD. At the end of last year I got an offer to join a company that has a physical piece of hardware as their product, with a react native app as client for this hardware. It didn't take me long to decide. It's a great company, with a great vision and roadmap.&lt;/p&gt;

&lt;p&gt;The codebase wasn't how I imagined. All components was class components, written in the "old" react way using class components with &lt;code&gt;.bind(this)&lt;/code&gt;. A global context that was one big mess. No TypeScript - or PropTypes for the components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - The cleanup
&lt;/h2&gt;

&lt;p&gt;One of my first pull requests was adding typescript and a testing framework and started to refactor most of the presentation components into functional components. In that process I also added some tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2 - Cleanup App.js
&lt;/h3&gt;

&lt;p&gt;Step 2 was splitting the HUGE (~800 lines including the global AppContext) &lt;code&gt;App.js&lt;/code&gt; into multiple components and separating the AppContext into a file by it self. After doing this the whole app started to perform much better. Step 2 was now done.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3 - Cleaning up the AppContext
&lt;/h3&gt;

&lt;p&gt;Code was already starting to look better. The app was performing better. The next step was a bit more cleanup in the AppContext. I wanted to extract some of the AppContext into separate contexts - I'd rater have less lines and many file than one very large file with many lines.&lt;/p&gt;

&lt;p&gt;One question I quickly had to ask ask my self was: "How do you have multiple contexts in a React class component?". I've spend some time reading up on this and didn't like the approach that was mentioned in the &lt;a href="https://reactjs.org/docs/context.html#consuming-multiple-contexts"&gt;react documentation - Consuming Multiple Contexts&lt;/a&gt;. I'm a lazy developer and it just seemed like too much work and hard to maintain.&lt;/p&gt;

&lt;p&gt;I've been working with Redux before and thought that I could use the same approach as Redux. Use a Higher Order Component to inject the context into the props of the component.&lt;/p&gt;

&lt;p&gt;Instead of making a new HOC for each context I wanted some generic that could be used for all my new contexts and all components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4 - The &lt;code&gt;withContext&lt;/code&gt; HOC
&lt;/h3&gt;

&lt;p&gt;So how should I go about creating this. I wanted to inject the values of the context into the component- that part was clear.&lt;/p&gt;

&lt;h4&gt;
  
  
  Let me present &lt;code&gt;useContext&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Component&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Props&lt;/span&gt; &lt;span class="na"&gt;extends&lt;/span&gt; &lt;span class="na"&gt;Record&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;string&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="na"&gt;any&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&amp;gt;(contextName: string, context: Context&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;any&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;, WrappedComponent: ComponentType&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;) =&amp;gt;
  (p: Props) =&amp;gt; &lt;span class="si"&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;WrappedComponent&lt;/span&gt;
        &lt;span class="si"&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;contextName&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;),&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="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="si"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyClassComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&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="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;Text&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;my&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Text&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;withContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;ConnectionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;MyClassComponent&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works very well and is very easy to implement.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first parameter of the HOC is the prop name that the context should have.&lt;/li&gt;
&lt;li&gt;The second parameter name is the actual context&lt;/li&gt;
&lt;li&gt;The third parameter is the component that the context should be injected to.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;withContext&lt;/code&gt; will allow me to grab &lt;code&gt;this.props.connection.isOnline&lt;/code&gt; to check if the phone is connected to the internet or not. To inject multiple contexts into my component I can use something like &lt;code&gt;compose&lt;/code&gt; to loop thru all &lt;code&gt;withContext&lt;/code&gt; and apply them to all of my class components.&lt;/p&gt;

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