<?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: Carlos Santiago</title>
    <description>The latest articles on DEV Community by Carlos Santiago (@csantiago132).</description>
    <link>https://dev.to/csantiago132</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%2F136628%2F6c1862ce-79f7-4a67-9515-e33c268c3799.jpeg</url>
      <title>DEV Community: Carlos Santiago</title>
      <link>https://dev.to/csantiago132</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/csantiago132"/>
    <language>en</language>
    <item>
      <title>How to migrate from Redux to React Context API</title>
      <dc:creator>Carlos Santiago</dc:creator>
      <pubDate>Wed, 26 Feb 2020 04:41:22 +0000</pubDate>
      <link>https://dev.to/csantiago132/how-to-migrate-from-redux-to-react-context-api-5gkc</link>
      <guid>https://dev.to/csantiago132/how-to-migrate-from-redux-to-react-context-api-5gkc</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;The main goal of this article is to replace Redux with React Context API.  We will achieve this by going to any connected component and replace a line of code with a &lt;code&gt;connect&lt;/code&gt; function that we will write. &lt;/p&gt;

&lt;p&gt;When you finish reading this article, you'll be able to migrate from Redux to React Context API smoothly, quicker, and without rewriting plenty of code. We will achieve our goal doing these five steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: Model your reducers in an object&lt;/li&gt;
&lt;li&gt;Step 2: Create the &lt;code&gt;combineReducer&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Step 3: Create the app's provider&lt;/li&gt;
&lt;li&gt;Step 4: Create the &lt;code&gt;connect&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Step 5: Connect the components to the provider&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;You will need some knowledge in React, Redux and React Context API. Also, some knowledge of Higher Order Functions and currying functions in javascript would be helpful. &lt;/p&gt;

&lt;h3&gt;
  
  
  Before we start...
&lt;/h3&gt;

&lt;p&gt;If you want to do this tutorial while you read, you can open this &lt;a href="https://codesandbox.io/s/react-redux-example-qzoro"&gt;CodeSandbox using React and Redux&lt;/a&gt; in a new tab, fork it and start coding. The CodeSandbox is embedded at the end of the article for easy reference.&lt;/p&gt;

&lt;h4&gt;
  
  
  Folder Structure
&lt;/h4&gt;

&lt;p&gt;Let's create the folder structure where we are going to place the code. This structure is a suggestion and should not dictate how you organize your code.&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;directory&lt;/span&gt;
  &lt;span class="err"&gt;└───&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;other&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;directories&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="err"&gt;───&lt;/span&gt; &lt;span class="nx"&gt;provider&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;        &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="nx"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;        &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;        &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="nx"&gt;reducers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="err"&gt;───&lt;/span&gt; &lt;span class="nx"&gt;utils&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;        &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="nx"&gt;combineReducers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
   &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="nx"&gt;other&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;directories&lt;/span&gt;

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

&lt;/div&gt;

&lt;h3&gt;
  
  
  Step 1: Model your reducers
&lt;/h3&gt;

&lt;p&gt;Go to &lt;code&gt;reducers.js&lt;/code&gt; and start placing all the reducers of the app with it's key inside an object.  &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Start writing the &lt;code&gt;combineReducer&lt;/code&gt; function
&lt;/h3&gt;

&lt;p&gt;First, let's start writing the function that will create the root reducer and the parameters it needs. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  2.1 • Start modeling the initial global state
&lt;/h4&gt;

&lt;p&gt;In this iteration, we will call each reducer to get its initial state. Pass &lt;code&gt;undefined&lt;/code&gt; as the &lt;code&gt;state&lt;/code&gt; parameter and anything you want as the &lt;code&gt;action&lt;/code&gt; parameter, each reducer will return the initial state provided. Then, the results are added to the &lt;code&gt;globalState&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries"&gt;Object.entries()&lt;/a&gt; gives us an array of key-value pairs from the &lt;code&gt;reducers&lt;/code&gt; object passed as a parameter. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  2.2 • Start writing the global reducer function
&lt;/h4&gt;

&lt;p&gt;Now, we are going to write the primary reducer function. We are writing this function to pass it to the &lt;a href="https://reactjs.org/docs/hooks-reference.html#usereducer"&gt;&lt;strong&gt;useReducer hook&lt;/strong&gt;&lt;/a&gt; later on. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  2.3 • Let’s update the global state
&lt;/h4&gt;

&lt;p&gt;The most important part of this function is to get the next state. We are going to iterate through each &lt;code&gt;reducer&lt;/code&gt; available and pass the &lt;code&gt;action&lt;/code&gt; parameter to get the next state returned by the reducer.&lt;/p&gt;

&lt;p&gt;In the iteration, we are going to compare the returned object with the current state. If these objects are not the same, it means there was an update, and we are going to replace the &lt;code&gt;state&lt;/code&gt; with the updated object.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Next, if the state has changed, we will return the updated state. If not, we return the previous state.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  2.4 • Finish writing the function
&lt;/h4&gt;

&lt;p&gt;Finally, we will return an array with the initial state and the global reducer function. These values will be passed to the &lt;strong&gt;useReducer&lt;/strong&gt; hook. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Step 3: Write the app's &lt;code&gt;Provider&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Let's write the app's provider. Then, import the object containing our reducers and the &lt;code&gt;combineReducer&lt;/code&gt; function from the previous step. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  3.1 • Wrap up the function in the &lt;strong&gt;useCallback&lt;/strong&gt; hook
&lt;/h4&gt;

&lt;p&gt;We are not expecting our initial state, or the objects containing the reducers, to change on each re-render. So, let's optimize our function by using the &lt;strong&gt;useCallback&lt;/strong&gt; hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;useCallback&lt;/strong&gt; will return a memoized version of the callback that only changes if one of the inputs has changed. There is no need for this function to run on every re-render.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  3.2 • Setup the rest of the provider
&lt;/h4&gt;

&lt;p&gt;Next, let's wrap up the provider by doing a few more steps. First, &lt;br&gt;
destructure the returned value of the &lt;strong&gt;useCallback&lt;/strong&gt; function and set up the &lt;strong&gt;useReducer&lt;/strong&gt; hook. &lt;/p&gt;

&lt;p&gt;Once that's done, create a &lt;strong&gt;useMemo&lt;/strong&gt; hook to wrap the returned value of the &lt;strong&gt;useReducer&lt;/strong&gt; hook. Why &lt;strong&gt;useMemo&lt;/strong&gt;? Since this is the global provider, there are two main reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your context value changes frequently&lt;/li&gt;
&lt;li&gt;Your context has many consumers&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  3.3 • Finish up the app's provider
&lt;/h4&gt;

&lt;p&gt;Finally, let's return the consumer and export the provider and have it ready to pass context to all the children below it. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Step 4: Start writing the &lt;code&gt;connect&lt;/code&gt; function
&lt;/h3&gt;

&lt;p&gt;The HOC function is the last function we will write before we start connecting the component to the provider. &lt;/p&gt;

&lt;p&gt;This function will pass the state and the global reducer to each component. This "connects" to React Context Provider API and lets our components consume the values given by it.&lt;/p&gt;

&lt;p&gt;The simplest use case of our function is a purely curried one and will take three parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mapStateToProps&lt;/code&gt; - required&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mapDispatchToProps&lt;/code&gt; - optional - not all components dispatch actions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Component&lt;/code&gt; - required &lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  4.2 • Return a connected component
&lt;/h4&gt;

&lt;p&gt;Let's place the &lt;code&gt;Context.Consumer&lt;/code&gt; to have access to the global state and dispatch function. Then, let's pass &lt;code&gt;value.state&lt;/code&gt; to the &lt;code&gt;mapStateToProps&lt;/code&gt;function.&lt;/p&gt;

&lt;p&gt;Remember, the &lt;code&gt;mapDispatchToProps&lt;/code&gt; parameter is optional. If you pass this parameter, pass &lt;code&gt;value.dispatch&lt;/code&gt; to the &lt;code&gt;mapDispatchToProps&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;Finally, let's combine all &lt;code&gt;props&lt;/code&gt; and add the final result to the component. Now this component is &lt;code&gt;connected&lt;/code&gt; to the React Context API. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Step 5: The last step: connect our components
&lt;/h3&gt;

&lt;p&gt;Now we can migrate from Redux to React Context Provider API quickly and with little refactoring on our part. &lt;/p&gt;

&lt;h5&gt;
  
  
  Replace the Redux &lt;code&gt;Provider&lt;/code&gt;
&lt;/h5&gt;

&lt;p&gt;Let's start by replacing the &lt;code&gt;Provider&lt;/code&gt; from Redux with the one we created. Your main app file should look like below:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h5&gt;
  
  
  Replace the Redux &lt;code&gt;connect&lt;/code&gt; function
&lt;/h5&gt;

&lt;p&gt;Finally, let's replace the &lt;code&gt;connect&lt;/code&gt; function imported from Redux with our &lt;code&gt;connect&lt;/code&gt; function. Your component should look like below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can access all the properties returned from &lt;code&gt;mapStateToProps&lt;/code&gt; and &lt;code&gt;mapDispatchToProps&lt;/code&gt; as &lt;code&gt;props&lt;/code&gt; inside the connected component. &lt;/p&gt;

&lt;p&gt;Lastly, refresh the page and the app should be connected to the React Context API. Repeat this step to all the components that you want to replace Redux with React Context Provider API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here is a CodeSandbox with all the code we wrote and connected to React Context API&lt;/strong&gt;&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/x4683?module=%2Fsrc%2Fprovider%2Fconnect.js"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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

&lt;p&gt;So there you have it, in five steps, we successfully moved away from Redux and replace it with React Context API.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model your reducers in an object&lt;/li&gt;
&lt;li&gt;Create the &lt;code&gt;combineReducer&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Create the app's provider&lt;/li&gt;
&lt;li&gt;Create the &lt;code&gt;useConnect&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;Connect the components to the provider&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Articles&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.taniarascia.com/using-context-api-in-react/"&gt;Using Context API in React (Hooks and Classes)&lt;/a&gt; by &lt;a href="https://twitter.com/taniarascia"&gt;Tania Rascia&lt;/a&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.sitepoint.com/currying-in-functional-javascript/"&gt;A Beginner’s Guide to Currying in Functional JavaScript&lt;/a&gt; by &lt;a href="https://www.sitepoint.com/author/mdavidgreen/"&gt;M. David Green&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CodeSanbox&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://codesandbox.io/s/react-redux-example-qzoro"&gt;Initial CodeSandbox using Redux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://codesandbox.io/s/react-context-provider-example-x4683"&gt;Final CodeSandbox using React Context API&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Github Repo&lt;/strong&gt; &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJ70wriM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-ba8488d21cd8ee1fee097b8410db9deaa41d0ca30b004c0c63de0a479114156f.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/csantiago132"&gt;
        csantiago132
      &lt;/a&gt; / &lt;a href="https://github.com/csantiago132/migrate-from-redux-to-react-context-provider"&gt;
        migrate-from-redux-to-react-context-provider
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Created with CodeSandbox
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>react</category>
      <category>redux</category>
      <category>context</category>
      <category>migrate</category>
    </item>
  </channel>
</rss>
