<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Thomas Brandt on Medium]]></title>
        <description><![CDATA[Stories by Thomas Brandt on Medium]]></description>
        <link>https://medium.com/@thomas_brandt?source=rss-9ffc4873fdf6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_3hh1vaG_T-XQB-Q56trRg.jpeg</url>
            <title>Stories by Thomas Brandt on Medium</title>
            <link>https://medium.com/@thomas_brandt?source=rss-9ffc4873fdf6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:32:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thomas_brandt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Create an International Website with Next.js and React]]></title>
            <link>https://javascript.plainenglish.io/how-to-create-an-international-website-with-next-js-and-react-hooks-14fec9c61fac?source=rss-9ffc4873fdf6------2</link>
            <guid isPermaLink="false">https://medium.com/p/14fec9c61fac</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Thomas Brandt]]></dc:creator>
            <pubDate>Fri, 20 Aug 2021 20:09:01 GMT</pubDate>
            <atom:updated>2021-08-21T20:07:11.930Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FJvTh21HifOpQrmV" /><figcaption>Photo by <a href="https://unsplash.com/@sonereker?utm_source=medium&amp;utm_medium=referral">Soner Eker</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Offer your website in multiple languages with a simple React custom hook</h4><h3>Introduction</h3><p>When creating a website with Next.js you often come across the problem of offering multiple languages on your website, sometimes you will use a CMS like Strapi to handle the different language outputs. In this case, however, the website is static (and non-dependent on a backend) and the text is stored directly in the HTML.</p><p>The solution I am offering uses the built-in <a href="https://nextjs.org/docs/advanced-features/i18n-routing">i18n tools</a> of Next.js using JSON or MarkDown files which are coupled with a custom react hook handling the different language outputs for you.</p><p>I will start by explaining how to prepare your folders and files, then how to add content in different languages, afterwards how to create and use the custom hook and finally, I will briefly explain how to change locales from within your UI.</p><h3>1. Preparing the website</h3><p>The first thing you should do is set up your Next.js app by following the well-made tutorial on the Next.js <a href="https://nextjs.org/learn/basics/create-nextjs-app/setup">Homepage</a>. Once you have done that we will start by making changes to the <strong>next.config.js</strong> file in the root of your app.</p><p>Depending on the languages you are going to add to your website your file will look a bit different to the one shown. However, the process will be the same. In this example, I will be adding french and german to the default English version.</p><p>Add the i18n module to your file, insert the locales, set the default locale and choose if yes or no you want your website to automatically detect the language of the user. (Don’t forget to restart your app if you want the changes in the file to take effect)</p><pre>module<strong>.</strong>exports <strong>=</strong> {</pre><pre>   reactStrictMode: true,</pre><pre>   i18n: {<br>      localeDetection: false,<br>      locales: [&quot;fr&quot;, &quot;en&quot;, &quot;de&quot;],<br>      defaultLocale: &quot;fr&quot;,<br>   },</pre><pre>}</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dmodule.exports%252520%25253D%252520%25257B%25250A%252520%252520reactStrictMode%25253A%252520true%25252C%25250A%252520%252520i18n%25253A%252520%25257B%25250A%252520%252520%252520%252520localeDetection%25253A%252520false%25252C%25250A%252520%252520%252520%252520locales%25253A%252520%25255B%252522fr%252522%25252C%252520%252522en%252522%25252C%252520%252522de%252522%25255D%25252C%25250A%252520%252520%252520%252520defaultLocale%25253A%252520%252522fr%252522%25252C%25250A%252520%252520%25257D%25252C%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dmodule.exports%25252520%2525253D%25252520%2525257B%2525250A%25252520%25252520reactStrictMode%2525253A%25252520true%2525252C%2525250A%25252520%25252520i18n%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520localeDetection%2525253A%25252520false%2525252C%2525250A%25252520%25252520%25252520%25252520locales%2525253A%25252520%2525255B%25252522fr%25252522%2525252C%25252520%25252522en%25252522%2525252C%25252520%25252522de%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520defaultLocale%2525253A%25252520%25252522fr%25252522%2525252C%2525250A%25252520%25252520%2525257D%2525252C%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b64261b2a38d10a4fed25043bd33e247/href">https://medium.com/media/b64261b2a38d10a4fed25043bd33e247/href</a></iframe><p>Once you have done that we will also add a system with which all of the files in our app will be much easier to access. To do that create/ modify the <strong>jsconfig.json</strong> file in the root of your app directory. (Don’t forget to restart your app if you want the changes in the file to take effect)</p><pre>{<br>&quot;compilerOptions&quot;: {<br>   &quot;baseUrl&quot;: &quot;.&quot;,<br>   &quot;paths&quot;: {<br>      &quot;@/components/*&quot;: [&quot;components/*&quot;],<br>      &quot;@/styles/*&quot;: [&quot;styles/*&quot;],<br>      &quot;@/context/*&quot;: [&quot;context/*&quot;],<br>      &quot;@/locales/*&quot;: [&quot;locales/*&quot;],<br>      &quot;@/pages/*&quot;: [&quot;pages/*&quot;],<br>      &quot;@/public/*&quot;: [&quot;public/*&quot;],<br>   }<br>}<br>}</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25257B%25250A%252520%252520%252522compilerOptions%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252522baseUrl%252522%25253A%252520%252522.%252522%25252C%25250A%252520%252520%252520%252520%252522paths%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252522%252540%25252Fcomponents%25252F%252A%252522%25253A%252520%25255B%252522components%25252F%252A%252522%25255D%25252C%25250A%252520%252520%252520%252520%252520%252520%252522%252540%25252Fstyles%25252F%252A%252522%25253A%252520%25255B%252522styles%25252F%252A%252522%25255D%25252C%25250A%252520%252520%252520%252520%252520%252520%252522%252540%25252Fcontext%25252F%252A%252522%25253A%252520%25255B%252522context%25252F%252A%252522%25255D%25252C%25250A%252520%252520%252520%252520%252520%252520%252522%252540%25252Flocales%25252F%252A%252522%25253A%252520%25255B%252522locales%25252F%252A%252522%25255D%25252C%25250A%252520%252520%252520%252520%252520%252520%252522%252540%25252Fpages%25252F%252A%252522%25253A%252520%25255B%252522pages%25252F%252A%252522%25255D%25252C%25250A%252520%252520%252520%252520%252520%252520%252522%252540%25252Fpublic%25252F%252A%252522%25253A%252520%25255B%252522public%25252F%252A%252522%25255D%25252C%25250A%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25250A%25257D%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%2525257B%2525250A%25252520%25252520%25252522compilerOptions%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252522baseUrl%25252522%2525253A%25252520%25252522.%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252522paths%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252540%2525252Fcomponents%2525252F%25252A%25252522%2525253A%25252520%2525255B%25252522components%2525252F%25252A%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252540%2525252Fstyles%2525252F%25252A%25252522%2525253A%25252520%2525255B%25252522styles%2525252F%25252A%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252540%2525252Fcontext%2525252F%25252A%25252522%2525253A%25252520%2525255B%25252522context%2525252F%25252A%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252540%2525252Flocales%2525252F%25252A%25252522%2525253A%25252520%2525255B%25252522locales%2525252F%25252A%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252540%2525252Fpages%2525252F%25252A%25252522%2525253A%25252520%2525255B%25252522pages%2525252F%25252A%25252522%2525255D%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252522%25252540%2525252Fpublic%2525252F%25252A%25252522%2525253A%25252520%2525255B%25252522public%2525252F%25252A%25252522%2525255D%2525252C%2525250A%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525250A%2525257D%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/19adc5969397fea704c04b4a15f43cef/href">https://medium.com/media/19adc5969397fea704c04b4a15f43cef/href</a></iframe><p>Now that you have done all the necessary modifications you will be able to access the locale data through your router object.</p><h3>2. Start adding your content</h3><p>Before starting to add your content you will need to understand how adding content will work. Because we won’t be using an external text editor we will store the content of the website in JavaScript files which will be imported into the components, based on what locale is currently active.</p><p>I store these files in a “locales” folder at the root of my app and have 3 files per page (each page has one file per language, in this case, English, french and german).</p><p>my_app/<br>├─ locales/<br>│ ├─ pages/<br>│ │ ├─ home/<br>│ │ │ ├─ en.js<br>│ │ │ ├─ fr.js<br>│ │ │ ├─ de.js<br>│ │ ├─ about/<br>│ │ ├─ contact/<br>│ ├─ components/</p><p>With each file having the same structure but other content.</p><pre>//en.js</pre><pre>export default { <br>   hero_text: &quot;This is the HERO&quot; <br>};<br></pre><pre>//fr.js</pre><pre>export default {<br>   hero_text: &quot;Ceci est le HERO&quot;,<br>};</pre><pre><br>//de.js</pre><pre>export default {<br>   hero_text: &quot;Das is der HERO&quot;<br>};</pre><p>You can do that with every page of your website, if you want to add markdown you can as well (as long as your use react-to-markdown to convert the markdown to HTML).</p><p>For this example, I have created a simple home page with a text that will change based on the current locale in use.</p><pre><strong>import</strong> HomeStyle <em>from</em> &quot;@/styles/Pages/Home/Home.module.scss&quot;;</pre><pre><strong>import</strong> { useLocales } <em>from</em> &quot;@/components/hooks/useLocales&quot;;<br></pre><pre>export default <em>function</em> Home() {</pre><pre><strong><em>   </em>const</strong> text <strong>=</strong> useLocales(&quot;home&quot;);<br>   <br>   <strong>return</strong> (<br>      &lt;&gt;</pre><pre>         &lt;main <em>className</em>={HomeStyle.container}&gt;</pre><pre>            &lt;h1&gt;{text.hero_text}&lt;/h1&gt;</pre><pre>         &lt;/main&gt;</pre><pre>      &lt;/&gt;</pre><pre>   );</pre><pre>}</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520HomeStyle%252520from%252520%252522%252540%25252Fstyles%25252FPages%25252FHome%25252FHome.module.scss%252522%25253B%25250A%25250Aimport%252520%25257B%252520useLocales%252520%25257D%252520from%252520%252522%252540%25252Fcomponents%25252Fhooks%25252FuseLocales%252522%25253B%25250A%25250Aexport%252520default%252520function%252520Home%2528%2529%252520%25257B%25250A%252520%252520const%252520text%252520%25253D%252520useLocales%2528%252522home%252522%2529%25253B%25250A%25250A%252520%252520return%252520%2528%25250A%252520%252520%252520%252520%25253C%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cmain%252520className%25253D%25257BHomeStyle.container%25257D%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25253Ch1%25253E%25257Btext.hero_text%25257D%25253C%25252Fh1%25253E%25250A%252520%252520%252520%252520%252520%252520%25253C%25252Fmain%25253E%25250A%252520%252520%252520%252520%25253C%25252F%25253E%25250A%252520%252520%2529%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520HomeStyle%25252520from%25252520%25252522%25252540%2525252Fstyles%2525252FPages%2525252FHome%2525252FHome.module.scss%25252522%2525253B%2525250A%2525250Aimport%25252520%2525257B%25252520useLocales%25252520%2525257D%25252520from%25252520%25252522%25252540%2525252Fcomponents%2525252Fhooks%2525252FuseLocales%25252522%2525253B%2525250A%2525250Aexport%25252520default%25252520function%25252520Home%252528%252529%25252520%2525257B%2525250A%25252520%25252520const%25252520text%25252520%2525253D%25252520useLocales%252528%25252522home%25252522%252529%2525253B%2525250A%2525250A%25252520%25252520return%25252520%252528%2525250A%25252520%25252520%25252520%25252520%2525253C%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cmain%25252520className%2525253D%2525257BHomeStyle.container%2525257D%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253Ch1%2525253E%2525257Btext.hero_text%2525257D%2525253C%2525252Fh1%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253C%2525252Fmain%2525253E%2525250A%25252520%25252520%25252520%25252520%2525253C%2525252F%2525253E%2525250A%25252520%25252520%252529%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30380668a877653fa173622d35f3365/href">https://medium.com/media/b30380668a877653fa173622d35f3365/href</a></iframe><p>As you can see I have already implemented the useLocales hook, in the next section I will explain how it works and what it does.</p><h3>3. The useLocales Hook</h3><p>The purpose of this hook is to be able to import the needed language file in every component without having to import each file individually in each component and having to create the conditional logic for every single one.</p><p>In your “components” folder add a folder called “hooks” and a file called <strong>useLocales.js</strong>.</p><p>my_app/<br>├─ locales/<br>├─ components/<br>│ ├─ hooks/<br>│ │ ├─ useLocales.js</p><p>Inside the file import useState, useEffect and useRouter from React and Next.js.</p><pre><em>import</em> { useState, useEffect } <em>from</em> &quot;react&quot;;</pre><pre><em>import</em> { useRouter } <em>from</em> &quot;next/router&quot;;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520useState%25252C%252520useEffect%252520%25257D%252520from%252520%252522react%252522%25253B%25250A%25250Aimport%252520%25257B%252520useRouter%252520%25257D%252520from%252520%252522next%25252Frouter%252522%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520useState%2525252C%25252520useEffect%25252520%2525257D%25252520from%25252520%25252522react%25252522%2525253B%2525250A%2525250Aimport%25252520%2525257B%25252520useRouter%25252520%2525257D%25252520from%25252520%25252522next%2525252Frouter%25252522%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b10cf8db64e0520df4f134b40764a6ba/href">https://medium.com/media/b10cf8db64e0520df4f134b40764a6ba/href</a></iframe><p>Once you have done that you can continue by creating the useLocales arrow function, destructuring the locale and creating a text state variable with useState. The text variable will then be returned to the components using the hook.</p><pre>export <strong><em>const</em></strong> useLocales <strong>=</strong> (path) <em>=&gt;</em> {</pre><pre><em>   </em><strong><em>const</em></strong> router <strong>=</strong> useRouter();<br>   <strong><em>const</em></strong> { locale } <strong>=</strong> router;</pre><pre><em>   </em><strong><em>const</em></strong> [text, setText] <strong>=</strong> useState({});</pre><pre><em>   </em><strong><em>return</em></strong> text;</pre><pre>};</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%252520const%252520useLocales%252520%25253D%252520%2528path%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520const%252520router%252520%25253D%252520useRouter%2528%2529%25253B%25250A%252520%252520%252520const%252520%25257B%252520locale%252520%25257D%252520%25253D%252520router%25253B%25250A%252520%252520%25250A%252520%252520%252520const%252520%25255Btext%25252C%252520setText%25255D%252520%25253D%252520useState%2528%25257B%25257D%2529%25253B%25250A%252520%252520%25250A%252520%252520%252520return%252520text%25253B%25250A%25257D%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%25252520const%25252520useLocales%25252520%2525253D%25252520%252528path%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520const%25252520router%25252520%2525253D%25252520useRouter%252528%252529%2525253B%2525250A%25252520%25252520%25252520const%25252520%2525257B%25252520locale%25252520%2525257D%25252520%2525253D%25252520router%2525253B%2525250A%25252520%25252520%2525250A%25252520%25252520%25252520const%25252520%2525255Btext%2525252C%25252520setText%2525255D%25252520%2525253D%25252520useState%252528%2525257B%2525257D%252529%2525253B%2525250A%25252520%25252520%2525250A%25252520%25252520%25252520return%25252520text%2525253B%2525250A%2525257D%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b4f803c35bca9f843336fc61f626521/href">https://medium.com/media/9b4f803c35bca9f843336fc61f626521/href</a></iframe><p>Now add a useEffect that will ensure the right locale will be given to the component and only if the component passes down a path. Adding the locale as a parameter will change the text each time the locale changes. For more information on dynamic imports visit the <a href="https://nextjs.org/docs/advanced-features/dynamic-import">Next.js documentation</a> on the subject.<em> In this example you see the importance of configuring paths in the first section, as you can see thanks to it the Hook is usable everywhere</em>.</p><pre>useEffect(() <em>=&gt;</em> {</pre><pre>   if (path) { </pre><pre>      import(`@/locales/pages/${path}/${locale}`)<strong>.</strong>then((t) <em>=&gt;</em> {<br>         setText(t<strong>.</strong>default);</pre><pre>   });</pre><pre>}<br>}, [locale]);</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520useEffect%2528%2528%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520if%252520%2528path%2529%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520import%2528%252560%252540%25252Flocales%25252Fpages%25252F%252524%25257Bpath%25257D%25252F%252524%25257Blocale%25257D%252560%2529.then%2528%2528t%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520setText%2528t.default%2529%25253B%25250A%252520%252520%252520%252520%252520%252520%25257D%2529%25253B%25250A%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25252C%252520%25255Blocale%25255D%2529%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520useEffect%252528%252528%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520if%25252520%252528path%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520import%252528%25252560%25252540%2525252Flocales%2525252Fpages%2525252F%25252524%2525257Bpath%2525257D%2525252F%25252524%2525257Blocale%2525257D%25252560%252529.then%252528%252528t%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520setText%252528t.default%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525252C%25252520%2525255Blocale%2525255D%252529%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac13fa846240156784c6e472bb432d5/href">https://medium.com/media/7ac13fa846240156784c6e472bb432d5/href</a></iframe><p>The completed hook looks like this.</p><pre><em>import</em> { useState, useEffect } <em>from</em> &quot;react&quot;;</pre><pre><em>import</em> { useRouter } <em>from</em> &quot;next/router&quot;;</pre><pre>export <strong><em>const</em></strong> useLocales <strong>=</strong> (path) <em>=&gt;</em> {</pre><pre><strong><em>   const</em></strong> router <strong>=</strong> useRouter();<br>   <strong><em>const</em></strong> { locale } <strong>=</strong> router;</pre><pre><strong><em>   const</em></strong> [text, setText] <strong>=</strong> useState({});</pre><pre>   useEffect(() <em>=&gt;</em> {</pre><pre>      if (path) {</pre><pre>         import(`@/locales/pages/${path}/${locale}`)<strong>.</strong>then((t) <em>=&gt;</em> {<br>         setText(t<strong>.</strong>default);</pre><pre>      });</pre><pre>   }<br>   }, [locale]);</pre><pre><strong><em>return</em></strong> text;</pre><pre>};</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520useState%25252C%252520useEffect%252520%25257D%252520from%252520%252522react%252522%25253B%25250A%25250Aimport%252520%25257B%252520useRouter%252520%25257D%252520from%252520%252522next%25252Frouter%252522%25253B%25250A%25250Aexport%252520const%252520useLocales%252520%25253D%252520%2528path%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520const%252520router%252520%25253D%252520useRouter%2528%2529%25253B%25250A%252520%252520const%252520%25257B%252520locale%252520%25257D%252520%25253D%252520router%25253B%25250A%25250A%252520%252520const%252520%25255Btext%25252C%252520setText%25255D%252520%25253D%252520useState%2528%25257B%25257D%2529%25253B%25250A%25250A%252520%252520useEffect%2528%2528%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520if%252520%2528path%2529%252520%25257B%25250A%25250A%252520%252520%252520%252520%252520%252520import%2528%252560%252540%25252Flocales%25252Fpages%25252F%252524%25257Bpath%25257D%25252F%252524%25257Blocale%25257D%252560%2529.then%2528%2528t%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520setText%2528t.default%2529%25253B%25250A%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%25257D%2529%25253B%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25252C%252520%25255Blocale%25255D%2529%25253B%25250A%25250A%252520%252520return%252520text%25253B%25250A%25257D%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520useState%2525252C%25252520useEffect%25252520%2525257D%25252520from%25252520%25252522react%25252522%2525253B%2525250A%2525250Aimport%25252520%2525257B%25252520useRouter%25252520%2525257D%25252520from%25252520%25252522next%2525252Frouter%25252522%2525253B%2525250A%2525250Aexport%25252520const%25252520useLocales%25252520%2525253D%25252520%252528path%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520const%25252520router%25252520%2525253D%25252520useRouter%252528%252529%2525253B%2525250A%25252520%25252520const%25252520%2525257B%25252520locale%25252520%2525257D%25252520%2525253D%25252520router%2525253B%2525250A%2525250A%25252520%25252520const%25252520%2525255Btext%2525252C%25252520setText%2525255D%25252520%2525253D%25252520useState%252528%2525257B%2525257D%252529%2525253B%2525250A%2525250A%25252520%25252520useEffect%252528%252528%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520if%25252520%252528path%252529%25252520%2525257B%2525250A%2525250A%25252520%25252520%25252520%25252520%25252520%25252520import%252528%25252560%25252540%2525252Flocales%2525252Fpages%2525252F%25252524%2525257Bpath%2525257D%2525252F%25252524%2525257Blocale%2525257D%25252560%252529.then%252528%252528t%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520setText%252528t.default%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525252C%25252520%2525255Blocale%2525255D%252529%2525253B%2525250A%2525250A%25252520%25252520return%25252520text%2525253B%2525250A%2525257D%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8858e5b0be757291cc2208c55f095b60/href">https://medium.com/media/8858e5b0be757291cc2208c55f095b60/href</a></iframe><h3>4. How to use the hook</h3><p>Going back to the <strong>index.js</strong> file you can see that you can simply import the hook using the simplified path and then extract the text object. Then, you can use the object by destructuring the text you have written in the JavaScript file.</p><pre><strong><em>import</em></strong> HomeStyle <em>from</em> &quot;@/styles/Pages/Home/Home.module.scss&quot;;<br><strong><em>import</em></strong> { useLocales } <em>from</em> &quot;@/components/hooks/useLocales&quot;;</pre><pre>export default <em>function</em> Home() {</pre><pre><strong><em>   const</em></strong> text <strong>=</strong> useLocales(&quot;home&quot;);<br>   <br>   <strong><em>return</em></strong> (<br>      &lt;&gt;</pre><pre>      &lt;main <em>className</em>={HomeStyle.container}&gt;</pre><pre>         &lt;h1&gt;{text.hero_text}&lt;/h1&gt;</pre><pre>      &lt;/main&gt;</pre><pre>     &lt;/&gt;</pre><pre>   );</pre><pre>}</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520HomeStyle%252520from%252520%252522%252540%25252Fstyles%25252FPages%25252FHome%25252FHome.module.scss%252522%25253B%25250A%25250Aimport%252520%25257B%252520useLocales%252520%25257D%252520from%252520%252522%252540%25252Fcomponents%25252Fhooks%25252FuseLocales%252522%25253B%25250A%25250Aexport%252520default%252520function%252520Home%2528%2529%252520%25257B%25250A%252520%252520const%252520text%252520%25253D%252520useLocales%2528%252522home%252522%2529%25253B%25250A%25250A%252520%252520return%252520%2528%25250A%252520%252520%252520%252520%25253C%25253E%25250A%252520%252520%252520%252520%252520%252520%25253Cmain%252520className%25253D%25257BHomeStyle.container%25257D%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25253Ch1%25253E%25257Btext.hero_text%25257D%25253C%25252Fh1%25253E%25250A%252520%252520%252520%252520%252520%252520%25253C%25252Fmain%25253E%25250A%252520%252520%252520%252520%25253C%25252F%25253E%25250A%252520%252520%2529%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520HomeStyle%25252520from%25252520%25252522%25252540%2525252Fstyles%2525252FPages%2525252FHome%2525252FHome.module.scss%25252522%2525253B%2525250A%2525250Aimport%25252520%2525257B%25252520useLocales%25252520%2525257D%25252520from%25252520%25252522%25252540%2525252Fcomponents%2525252Fhooks%2525252FuseLocales%25252522%2525253B%2525250A%2525250Aexport%25252520default%25252520function%25252520Home%252528%252529%25252520%2525257B%2525250A%25252520%25252520const%25252520text%25252520%2525253D%25252520useLocales%252528%25252522home%25252522%252529%2525253B%2525250A%2525250A%25252520%25252520return%25252520%252528%2525250A%25252520%25252520%25252520%25252520%2525253C%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253Cmain%25252520className%2525253D%2525257BHomeStyle.container%2525257D%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253Ch1%2525253E%2525257Btext.hero_text%2525257D%2525253C%2525252Fh1%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525253C%2525252Fmain%2525253E%2525250A%25252520%25252520%25252520%25252520%2525253C%2525252F%2525253E%2525250A%25252520%25252520%252529%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30380668a877653fa173622d35f3365/href">https://medium.com/media/b30380668a877653fa173622d35f3365/href</a></iframe><p>The useLocales hooks automatically detect changes to the current locale (you can change the locale manually by adding “fr” or “de” to the end of your URL, for example: “localhost:3000/fr”).</p><h3>5. How to change the locale</h3><p>Now that you have installed your favourite locales onto your website and they automatically switch you will need to be able to switch them from within your UI. To do that you have two solutions:</p><p><strong>1. Using the “Link” component</strong></p><p>Simply import the “Link” from “next/link” and insert it into your code with an “href” parameter linking to your desired location and a “locale” parameter with your chosen locale (code found on nextjs.org).</p><pre>import Link from &#39;next/link&#39;<br><br>export default function IndexPage(props) {<br>  <strong>return</strong> (<br>    &lt;Link href=&quot;/another&quot; locale=&quot;fr&quot;&gt;<br>      &lt;a&gt;To /fr/another&lt;/a&gt;<br>    &lt;/Link&gt;<br>  )<br>}</pre><p><strong>2. Using “next router”</strong></p><p>Instead of using a component you can simply import the router from next/router (as we did before) and use the push method by adding a locale parameter (code found on nextjs.org).</p><pre>import { useRouter } from &#39;next/router&#39;<br><br>export default function IndexPage(props) {<br>  const router = useRouter()<br><br>  return (<br>    &lt;div<br>      onClick={() =&gt; {<br>        router.push(&#39;/another&#39;, &#39;/another&#39;, { locale: &#39;fr&#39; })<br>      }}<br>    &gt;<br>      to /fr/another<br>    &lt;/div&gt;<br>  )<br>}</pre><p>For more informations on how to do that visit: <a href="https://nextjs.org/docs/advanced-features/i18n-routing#transition-between-locales">https://nextjs.org/docs/advanced-features/i18n-routing#transition-between-locales</a></p><p>You are now all set to create your international website! If you should have any question on the subject feel free to contact me. Feedback is also always welcome!</p><h4>Sources</h4><ul><li>i18n explanations found on <a href="https://nextjs.org/docs/advanced-features/i18n-routing">https://nextjs.org/docs/advanced-features/i18n-routing</a></li><li>How to create a custom react hook on the react website under <a href="https://reactjs.org/docs/hooks-custom.html">https://reactjs.org/docs/hooks-custom.html</a></li><li>Image from <a href="https://unsplash.com/">Unsplash</a></li><li><a href="https://carbon.now.sh/">Carbon</a> for the code embedding</li></ul><p><em>More content at </em><a href="http://plainenglish.io/"><strong><em>plainenglish.io</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=14fec9c61fac" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/how-to-create-an-international-website-with-next-js-and-react-hooks-14fec9c61fac">How to Create an International Website with Next.js and React</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Context, useReducer and LocalStorage in Next JS]]></title>
            <link>https://medium.com/geekculture/how-to-use-context-usereducer-and-localstorage-in-next-js-cc7bc925d3f2?source=rss-9ffc4873fdf6------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc7bc925d3f2</guid>
            <category><![CDATA[usereducer]]></category>
            <category><![CDATA[usecontext]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Thomas Brandt]]></dc:creator>
            <pubDate>Tue, 17 Aug 2021 15:19:43 GMT</pubDate>
            <atom:updated>2021-08-19T18:36:32.834Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ynat4ynJ2wmzXZym" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Access state from anywhere within your app and keep the data on reload</h4><p>Having a global state object accessible from every place of your application comes in rather handy in any type of project. Being able to use a reducer and keep everything in LocalStorage is even more useful.</p><p>To do that we’ll be looking at the React useContext and useReducer Hook.</p><p>The first thing you are going to want to do is to create a Next JS project with the very goods documentation over at <a href="https://nextjs.org/docs">https://nextjs.org/docs</a> .</p><p>We’ll start by looking at the Context, then the Reducer and finally at the LocalStorage.</p><h3>1. Create the Context</h3><p>Once you’ve created a basic Next js app you will have to create a “context” folder and add an “AppContext.js” file to it. the context is the part that will allow you to share a state with the entire app and the “AppContext.js” file will hold the context and a wrapper to share the data.</p><p>my_app/<br>├─ context/<br>│ ├─ AppContext.js</p><p>First, import all the necessary Hooks from React.</p><pre><strong>import</strong> { createContext, useContext, useMemo } from &quot;react&quot;;</pre><p>Then comes the AppContext variable to create the Context and the AppWrapper which will pass down the context to the rest of the app and which will be exported from this file.</p><pre><strong>import</strong> { createContext, useContext, useMemo } from &quot;react&quot;;</pre><pre><br><strong>const</strong> AppContext = createContext();<br></pre><pre><strong><em>export</em></strong><em> </em><strong><em>function</em></strong> AppWrapper({ children }) {</pre><pre><em>   </em><strong><em>const</em></strong> [appState, setAppState] <strong>=</strong> useState({});</pre><pre><em>   </em><strong><em>const</em></strong> contextValue <strong>=</strong> useMemo(() <em>=&gt;</em> {</pre><pre>      <strong><em>return</em></strong> [appState, setAppState];</pre><pre>   }, [appState, setAppState]);</pre><pre><em><br>   </em><strong><em>return</em></strong> (</pre><pre>   &lt;AppContext.Provider <em>value</em>={contextValue}&gt;<br>      {children}<br>   &lt;/AppContext.Provider&gt;</pre><pre>   );</pre><pre>}</pre><pre><strong>export</strong> <em>function</em> useAppContext() {</pre><pre><strong><em>   return</em></strong> useContext(AppContext);</pre><pre>}</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520createContext%25252C%252520useContext%25252C%252520useMemo%252520%25257D%252520from%252520%252522react%252522%25253B%25250A%25250Aconst%252520AppContext%252520%25253D%252520createContext%2528%2529%25253B%25250A%25250Aexport%252520function%252520AppWrapper%2528%25257B%252520children%252520%25257D%2529%252520%25257B%25250A%252520%252520const%252520%25255BAppState%25252C%252520setAppState%25255D%252520%25253D%252520useState%2528%25257B%25257D%2529%25253B%25250A%25250A%252520%252520const%252520contextValue%252520%25253D%252520useMemo%2528%2528%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520return%252520%25255BAppState%25252C%252520setAppState%25255D%25253B%25250A%252520%252520%25257D%25252C%252520%25255BAppState%25252C%252520setAppState%25255D%2529%25253B%25250A%25250A%252520%252520return%252520%2528%25250A%252520%252520%252520%252520%25253CAppContext.Provider%252520value%25253D%25257BcontextValue%25257D%25253E%25257Bchildren%25257D%25253C%25252FAppContext.Provider%25253E%25250A%252520%252520%2529%25253B%25250A%25257D%25250A%25250Aexport%252520function%252520useAppContext%2528%2529%252520%25257B%25250A%252520%252520return%252520useContext%2528AppContext%2529%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520createContext%2525252C%25252520useContext%2525252C%25252520useMemo%25252520%2525257D%25252520from%25252520%25252522react%25252522%2525253B%2525250A%2525250Aconst%25252520AppContext%25252520%2525253D%25252520createContext%252528%252529%2525253B%2525250A%2525250Aexport%25252520function%25252520AppWrapper%252528%2525257B%25252520children%25252520%2525257D%252529%25252520%2525257B%2525250A%25252520%25252520const%25252520%2525255BAppState%2525252C%25252520setAppState%2525255D%25252520%2525253D%25252520useState%252528%2525257B%2525257D%252529%2525253B%2525250A%2525250A%25252520%25252520const%25252520contextValue%25252520%2525253D%25252520useMemo%252528%252528%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520return%25252520%2525255BAppState%2525252C%25252520setAppState%2525255D%2525253B%2525250A%25252520%25252520%2525257D%2525252C%25252520%2525255BAppState%2525252C%25252520setAppState%2525255D%252529%2525253B%2525250A%2525250A%25252520%25252520return%25252520%252528%2525250A%25252520%25252520%25252520%25252520%2525253CAppContext.Provider%25252520value%2525253D%2525257BcontextValue%2525257D%2525253E%2525257Bchildren%2525257D%2525253C%2525252FAppContext.Provider%2525253E%2525250A%25252520%25252520%252529%2525253B%2525250A%2525257D%2525250A%2525250Aexport%25252520function%25252520useAppContext%252528%252529%25252520%2525257B%2525250A%25252520%25252520return%25252520useContext%252528AppContext%252529%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2dac2a887fa7aa67e4f3e84e973c6fd/href">https://medium.com/media/d2dac2a887fa7aa67e4f3e84e973c6fd/href</a></iframe><p>Within the AppWrapper we define an AppState state variable and use the useState hook to initialise it (this is where your state data goes and you can then use the setAppState method to modify it). With the useMemo hook we then memoize the state value for better performance. Then we return a provider that we can use to output the rest of the App with the context value. Finally, we export the useAppContext function which we will then be able to import from everywhere in the App to get the state data.</p><p>Now, navigate to the “pages” and “_app.js”, there we will import our AppWrapper and wrap our Layout or App with it.</p><p>my_app/<br>├─ context/<br>│ ├─ AppContext.js<br>├─ pages/<br>│ ├─ _app.js</p><pre><strong>import</strong> &quot;@/styles/globals.scss&quot;;<br><strong>import</strong> Layout from &quot;@/components/Layout&quot;;</pre><pre><strong>import</strong> { AppWrapper } from &quot;@/context/AppContext&quot;;</pre><pre><strong>function</strong> MyApp({Component, pageProps}) {<br>   <strong>return</strong> (<br>      &lt;&gt;<br>         &lt;AppWrapper&gt;<br>            &lt;Layout&gt;<br>               &lt;Component {...pageProps} /&gt;<br>            &lt;/Layout&gt;<br>         &lt;/AppWrapper&gt;<br>      &lt;/&gt;<br>   );<br>}</pre><pre><strong>export</strong> default MyApp;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%252522%252540%25252Fstyles%25252Fglobals.scss%252522%25253B%25250Aimport%252520Layout%252520from%252520%252522%252540%25252Fcomponents%25252FLayout%252522%25253B%25250A%25250Aimport%252520%25257B%252520AppWrapper%252520%25257D%252520from%252520%252522%252540%25252Fcontext%25252FAppContext%252522%25253B%25250A%25250Afunction%252520MyApp%2528%25257B%252520Component%25252C%252520pageProps%252520%25257D%2529%252520%25257B%25250A%25250A%252520%252520return%252520%2528%25250A%252520%252520%252520%252520%25253C%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25253CAppWrapper%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25253CLayout%25253E%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25253CComponent%252520%25257B...pageProps%25257D%252520%25252F%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%252520%25253C%25252FLayout%25253E%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25253C%25252FAppWrapper%25253E%25250A%252520%252520%252520%252520%25253C%25252F%25253E%25250A%252520%252520%2529%25253B%25250A%25257D%25250A%25250Aexport%252520default%252520MyApp%25253B%25250A&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%25252522%25252540%2525252Fstyles%2525252Fglobals.scss%25252522%2525253B%2525250Aimport%25252520Layout%25252520from%25252520%25252522%25252540%2525252Fcomponents%2525252FLayout%25252522%2525253B%2525250A%2525250Aimport%25252520%2525257B%25252520AppWrapper%25252520%2525257D%25252520from%25252520%25252522%25252540%2525252Fcontext%2525252FAppContext%25252522%2525253B%2525250A%2525250Afunction%25252520MyApp%252528%2525257B%25252520Component%2525252C%25252520pageProps%25252520%2525257D%252529%25252520%2525257B%2525250A%2525250A%25252520%25252520return%25252520%252528%2525250A%25252520%25252520%25252520%25252520%2525253C%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253CAppWrapper%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253CLayout%2525253E%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253CComponent%25252520%2525257B...pageProps%2525257D%25252520%2525252F%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253C%2525252FLayout%2525253E%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525253C%2525252FAppWrapper%2525253E%2525250A%25252520%25252520%25252520%25252520%2525253C%2525252F%2525253E%2525250A%25252520%25252520%252529%2525253B%2525250A%2525257D%2525250A%2525250Aexport%25252520default%25252520MyApp%2525253B%2525250A&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0f6c293422e887d96918810ac57bd6a/href">https://medium.com/media/b0f6c293422e887d96918810ac57bd6a/href</a></iframe><p>Once you have done this you will be able to use the context anywhere in your app with the useAppContext hook.</p><pre><strong>import</strong> { useAppContext } from &quot;@/context/AppContext&quot;;</pre><pre><strong>const</strong> [appState, setAppState] = useAppContext();</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520useAppContext%252520%25257D%252520from%252520%252522%252540%25252Fcontext%25252FAppContext%252522%25253B%25250A%25250Aconst%252520%25255BappState%25252C%252520setAppState%25255D%252520%25253D%252520useAppContext%2528%2529%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520useAppContext%25252520%2525257D%25252520from%25252520%25252522%25252540%2525252Fcontext%2525252FAppContext%25252522%2525253B%2525250A%2525250Aconst%25252520%2525255BappState%2525252C%25252520setAppState%2525255D%25252520%2525253D%25252520useAppContext%252528%252529%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ec6e29c9fed42d0b3f22122e9c02479/href">https://medium.com/media/6ec6e29c9fed42d0b3f22122e9c02479/href</a></iframe><p>Now that you are done with the context, we will be able to add the useReducer hook to improve the state usability.</p><h3>2. Add the useReducer hook</h3><p>Create a file called “AppReducer.js” next to “AppContext.js”, it will contain an initialState variable and an AppReducer arrow function. The initialState variable will be used to store the initial state of the app and the arrow function will be used to store the dispatch methods used to modify the state.</p><pre><strong>export</strong> <strong>const</strong> initialState = {<br>   number: 0,<br>};</pre><pre><strong>export</strong> <strong>const</strong> AppReducer = (state, action) =&gt; {<br>   switch (action.type){<br>      case &quot;add_number&quot;: {<br>         <strong>return</strong> {<br>            ...state,<br>            number: action.value + state.number,<br>         };<br>      }<br>   }<br>};</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%252520const%252520initialState%252520%25253D%252520%25257B%25250A%252520%252520number%25253A%2525200%25252C%25250A%25257D%25253B%25250A%25250Aexport%252520const%252520AppReducer%252520%25253D%252520%2528state%25252C%252520action%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520switch%252520%2528action.type%2529%252520%25257B%25250A%252520%252520%252520%252520case%252520%252522add_number%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520return%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520...state%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520number%25253A%252520action.value%252520%25252B%252520state.number%25252C%25250A%252520%252520%252520%252520%252520%252520%25257D%25253B%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25250A%25257D%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%25252520const%25252520initialState%25252520%2525253D%25252520%2525257B%2525250A%25252520%25252520number%2525253A%252525200%2525252C%2525250A%2525257D%2525253B%2525250A%2525250Aexport%25252520const%25252520AppReducer%25252520%2525253D%25252520%252528state%2525252C%25252520action%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520switch%25252520%252528action.type%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520case%25252520%25252522add_number%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520return%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520...state%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520number%2525253A%25252520action.value%25252520%2525252B%25252520state.number%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525250A%2525257D%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3cc99517edf3803650ff70fa3dda368/href">https://medium.com/media/f3cc99517edf3803650ff70fa3dda368/href</a></iframe><p>In this example, the initialState stores a “number” of value 0 and the AppReducer has a dispatch method called “add_number” which can be called and which increases the number in the state by any value dispatched with the method.</p><p>Now that we have the initialState and the reducer we can import them in the context and replace the useState with a useReducer using these two as arguments. (don’t forget to also import useReducer from React to be able to use it)</p><pre><strong><em>import</em></strong> { createContext, useContext, useMemo, useReducer } <em>from</em> &quot;react&quot;;<br><strong><em>import</em></strong> { AppReducer, initialState } <em>from</em> &quot;./AppReducer&quot;;</pre><pre><strong><em><br>const</em></strong> AppContext <strong>=</strong> createContext();</pre><pre><strong>export</strong> <em>function</em> AppWrapper({ children }) {</pre><pre><strong><em>   const</em></strong> { state, dispatch } <strong>=</strong> useReducer(AppReducer, initialState);</pre><pre><strong><em>   const</em></strong> contextValue <strong>=</strong> useMemo(() <em>=&gt;</em> {<br>      <strong><em>return</em></strong> { state, dispatch };<br>   }, [state, dispatch]);</pre><pre><strong><em>   <br>   return</em></strong> (<br>      &lt;AppContext.Provider <em>value</em><strong>=</strong>{contextValue}&gt;<br>         {children}<br>      &lt;/AppContext.Provider&gt;<br>   );</pre><pre>}<br></pre><pre><strong>export</strong> <em>function</em> useAppContext() {<br>   <strong><em>return</em></strong> useContext(AppContext);<br>}</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520createContext%25252C%252520useContext%25252C%252520useMemo%25252C%252520useReducer%252520%25257D%252520from%252520%252522react%252522%25253B%25250A%25250Aimport%252520%25257B%252520AppReducer%25252C%252520initialState%252520%25257D%252520from%252520%252522.%25252FAppReducer%252522%25253B%25250A%25250Aconst%252520AppContext%252520%25253D%252520createContext%2528%2529%25253B%25250A%25250Aexport%252520function%252520AppWrapper%2528%25257B%252520children%252520%25257D%2529%252520%25257B%25250A%252520%252520const%252520%25257B%252520state%25252C%252520dispatch%252520%25257D%252520%25253D%252520useReducer%2528AppReducer%25252C%252520initialState%2529%25253B%25250A%25250A%252520%252520const%252520contextValue%252520%25253D%252520useMemo%2528%2528%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520return%252520%25257B%252520state%25252C%252520dispatch%252520%25257D%25253B%25250A%252520%252520%25257D%25252C%252520%25255Bstate%25252C%252520dispatch%25255D%2529%25253B%25250A%25250A%252520%252520return%252520%2528%25250A%252520%252520%252520%252520%25253CAppContext.Provider%252520value%25253D%25257BcontextValue%25257D%25253E%25257Bchildren%25257D%25253C%25252FAppContext.Provider%25253E%25250A%252520%252520%2529%25253B%25250A%25257D%25250A%25250Aexport%252520function%252520useAppContext%2528%2529%252520%25257B%25250A%252520%252520return%252520useContext%2528AppContext%2529%25253B%25250A%25257D&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520createContext%2525252C%25252520useContext%2525252C%25252520useMemo%2525252C%25252520useReducer%25252520%2525257D%25252520from%25252520%25252522react%25252522%2525253B%2525250A%2525250Aimport%25252520%2525257B%25252520AppReducer%2525252C%25252520initialState%25252520%2525257D%25252520from%25252520%25252522.%2525252FAppReducer%25252522%2525253B%2525250A%2525250Aconst%25252520AppContext%25252520%2525253D%25252520createContext%252528%252529%2525253B%2525250A%2525250Aexport%25252520function%25252520AppWrapper%252528%2525257B%25252520children%25252520%2525257D%252529%25252520%2525257B%2525250A%25252520%25252520const%25252520%2525257B%25252520state%2525252C%25252520dispatch%25252520%2525257D%25252520%2525253D%25252520useReducer%252528AppReducer%2525252C%25252520initialState%252529%2525253B%2525250A%2525250A%25252520%25252520const%25252520contextValue%25252520%2525253D%25252520useMemo%252528%252528%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520return%25252520%2525257B%25252520state%2525252C%25252520dispatch%25252520%2525257D%2525253B%2525250A%25252520%25252520%2525257D%2525252C%25252520%2525255Bstate%2525252C%25252520dispatch%2525255D%252529%2525253B%2525250A%2525250A%25252520%25252520return%25252520%252528%2525250A%25252520%25252520%25252520%25252520%2525253CAppContext.Provider%25252520value%2525253D%2525257BcontextValue%2525257D%2525253E%2525257Bchildren%2525257D%2525253C%2525252FAppContext.Provider%2525253E%2525250A%25252520%25252520%252529%2525253B%2525250A%2525257D%2525250A%2525250Aexport%25252520function%25252520useAppContext%252528%252529%25252520%2525257B%2525250A%25252520%25252520return%25252520useContext%252528AppContext%252529%2525253B%2525250A%2525257D&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/daa1f51e64a06934cb5cb23e2c300093/href">https://medium.com/media/daa1f51e64a06934cb5cb23e2c300093/href</a></iframe><p>You have now added the useReducer hook to your App context, to use it the procedure is similar to how we used the useState variable and method in the App. Just import the useAppContext hook and instead of the AppState and setAppState destructure the state and dispatch.</p><pre><strong>import</strong> { useAppContext } from &quot;@/context/AppContext&quot;;</pre><pre><strong>const</strong> { state, dispatch } = useAppContext();</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%252520useAppContext%252520%25257D%252520from%252520%252522%252540%25252Fcontext%25252FAppContext%252522%25253B%25250A%25250Aconst%252520%25257B%252520state%25252C%252520dispatch%252520%25257D%252520%25253D%252520useAppContext%2528%2529%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%25252520useAppContext%25252520%2525257D%25252520from%25252520%25252522%25252540%2525252Fcontext%2525252FAppContext%25252522%2525253B%2525250A%2525250Aconst%25252520%2525257B%25252520state%2525252C%25252520dispatch%25252520%2525257D%25252520%2525253D%25252520useAppContext%252528%252529%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1047b1774667895cb940dfa84d0a96b1/href">https://medium.com/media/1047b1774667895cb940dfa84d0a96b1/href</a></iframe><p>And this is how you access the data and method (for more information on the useReducer hook visit <a href="https://reactjs.org/docs/hooks-reference.html#usereducer">https://reactjs.org/docs/hooks-reference.html#usereducer</a> .</p><pre><strong>const</strong> { number } = state;</pre><pre>dispatch({type: &quot;add_number&quot;, value: 3)};</pre><pre>console.log(number);</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%252520%25257B%252520number%252520%25257D%252520%25253D%252520state%25253B%25250A%25250Adispatch%2528%25257Btype%25253A%252520%252522add_number%252522%25252C%252520value%25253A%2525203%25257D%2529%25253B%25250A%25250Aconsole.log%2528number%2529%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dconst%25252520%2525257B%25252520number%25252520%2525257D%25252520%2525253D%25252520state%2525253B%2525250A%2525250Adispatch%252528%2525257Btype%2525253A%25252520%25252522add_number%25252522%2525252C%25252520value%2525253A%252525203%2525257D%252529%2525253B%2525250A%2525250Aconsole.log%252528number%252529%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8211d712232c9047a6c03d6878930be/href">https://medium.com/media/e8211d712232c9047a6c03d6878930be/href</a></iframe><p>Now that you have access to your data and can modify it from everywhere in your App, let’s add a way for it to stay after reload.</p><h3>3. Keep your data in LocalStorage</h3><p>Being able to keep the state after reloading the page is very useful, for example, if you want to create a food ordering app and you want to keep your basket after visiting another page.</p><p>I will be using the useEffect hook, another solution would have been to create a custom useLocalStorage hook, unfortunately using a hook within a hook is usually more complex than it is useful.</p><p>The first step will be to import the useEffect hook from React inside of the AppContext.js file.</p><pre><strong>import</strong> {useEffect, useReducer, createContext, useContext, useMemo } from &quot;react&quot;;</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%252520%25257B%25250A%252520%252520useEffect%25252C%25250A%252520%252520useReducer%25252C%25250A%252520%252520createContext%25252C%25250A%252520%252520useContext%25252C%25250A%252520%252520useMemo%25252C%25250A%25257D%252520from%252520%252522react%252522%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dimport%25252520%2525257B%2525250A%25252520%25252520useEffect%2525252C%2525250A%25252520%25252520useReducer%2525252C%2525250A%25252520%25252520createContext%2525252C%2525250A%25252520%25252520useContext%2525252C%2525250A%25252520%25252520useMemo%2525252C%2525250A%2525257D%25252520from%25252520%25252522react%25252522%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e92647cc2c90edf70ca1f7d5762e4d3/href">https://medium.com/media/3e92647cc2c90edf70ca1f7d5762e4d3/href</a></iframe><p>Now let’s create two useEffect hooks, one to check if there is already a state inside the local storage (and update the state if there is) and another to update the localstorage with the latest data. for more information on the useEffect hook <a href="https://reactjs.org/docs/hooks-effect.html">https://reactjs.org/docs/hooks-effect.html</a> .</p><pre>useEffect(() <em>=&gt;</em> {</pre><pre>   if (<em>JSON</em><strong>.</strong>parse(localStorage<strong>.</strong>getItem(&quot;state&quot;))) { <br>   <br>      <em>//checking if there already is a state in localstorage<br>      //if yes, update the current state with the stored one</em></pre><pre><em>      </em>dispatch({ <br>         type: &quot;init_stored&quot;, <br>         value: <em>JSON</em><strong>.</strong>parse(localStorage<strong>.</strong>getItem(&quot;state&quot;)),<br>      });</pre><pre>   }</pre><pre>}, []);</pre><pre>useEffect(() <em>=&gt;</em> {</pre><pre>   if (state <em>!==</em> initialState) {<br>      <br>      localStorage<strong>.</strong>setItem(&quot;state&quot;, <em>JSON</em><strong>.</strong>stringify(state)); <br>   <br>      <em>//create and/or set a new localstorage variable called &quot;state&quot;</em></pre><pre>   }</pre><pre>}, [state]);</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%252520useEffect%2528%2528%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520if%252520%2528JSON.parse%2528localStorage.getItem%2528%252522state%252522%2529%2529%2529%252520%25257B%252520%25250A%252520%252520%252520%252520%252520%252520%25252F%25252Fchecking%252520if%252520there%252520already%252520is%252520a%252520state%252520in%252520localstorage%25250A%252520%252520%252520%252520%252520%252520dispatch%2528%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520type%25253A%252520%252522init_stored%252522%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520value%25253A%252520JSON.parse%2528localStorage.getItem%2528%252522state%252522%2529%2529%25252C%252520%25250A%252520%252520%252520%252520%252520%252520%252520%252520%25252F%25252Fif%252520yes%25252C%252520update%252520the%252520current%252520state%252520with%252520the%252520stored%252520one%25250A%252520%252520%252520%252520%252520%252520%25257D%2529%25253B%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25252C%252520%25255B%25255D%2529%25253B%25250A%25250A%252520%252520useEffect%2528%2528%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520%252520%252520if%252520%2528state%252520%2521%25253D%25253D%252520initialState%2529%252520%25257B%25250A%252520%252520%252520%252520%252520%252520localStorage.setItem%2528%252522state%252522%25252C%252520JSON.stringify%2528state%2529%2529%25253B%252520%25250A%252520%252520%252520%252520%252520%252520%25252F%25252Fcreate%252520and%25252For%252520set%252520a%252520new%252520localstorage%252520variable%252520called%252520%252522state%252522%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25252C%252520%25255Bstate%25255D%2529%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3D%25252520useEffect%252528%252528%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520if%25252520%252528JSON.parse%252528localStorage.getItem%252528%25252522state%25252522%252529%252529%252529%25252520%2525257B%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252Fchecking%25252520if%25252520there%25252520already%25252520is%25252520a%25252520state%25252520in%25252520localstorage%2525250A%25252520%25252520%25252520%25252520%25252520%25252520dispatch%252528%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520type%2525253A%25252520%25252522init_stored%25252522%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520value%2525253A%25252520JSON.parse%252528localStorage.getItem%252528%25252522state%25252522%252529%252529%2525252C%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252Fif%25252520yes%2525252C%25252520update%25252520the%25252520current%25252520state%25252520with%25252520the%25252520stored%25252520one%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%252529%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525252C%25252520%2525255B%2525255D%252529%2525253B%2525250A%2525250A%25252520%25252520useEffect%252528%252528%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520if%25252520%252528state%25252520%252521%2525253D%2525253D%25252520initialState%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520localStorage.setItem%252528%25252522state%25252522%2525252C%25252520JSON.stringify%252528state%252529%252529%2525253B%25252520%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525252F%2525252Fcreate%25252520and%2525252For%25252520set%25252520a%25252520new%25252520localstorage%25252520variable%25252520called%25252520%25252522state%25252522%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525252C%25252520%2525255Bstate%2525255D%252529%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/55bf7ff5b2a10cc1c65ccb5a6a6db7bf/href">https://medium.com/media/55bf7ff5b2a10cc1c65ccb5a6a6db7bf/href</a></iframe><p>In the first hook, I use the dispatch method to call “init_stored” from the reducer which looks like this and which replaces the entire state with the stored state.</p><pre><strong>export</strong> <strong>const</strong> initialState = {<br>   number: 0,<br>};</pre><pre><strong>export</strong> <strong>const</strong> AppReducer = (state, action) =&gt; {<br>   switch (action.type){</pre><pre>      case &quot;init_stored&quot;: {<br>         <strong>return</strong> action.value,<br>      }</pre><pre>      case &quot;add_number&quot;: {<br>         <strong>return</strong> {<br>            ...state,<br>            number: action.value + state.number,<br>         };<br>      }<br>   }<br>};</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcarbon.now.sh%2Fembed%3Fbg%3Drgba%2528171%252C%2B184%252C%2B195%252C%2B1%2529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%2525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%252520const%252520initialState%252520%25253D%252520%25257B%25250A%252520%252520number%25253A%2525200%25252C%25250A%25257D%25253B%25250A%25250Aexport%252520const%252520AppReducer%252520%25253D%252520%2528state%25252C%252520action%2529%252520%25253D%25253E%252520%25257B%25250A%252520%252520switch%252520%2528action.type%2529%252520%25257B%25250A%252520%252520%252520%252520case%252520%252522init_stored%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520return%252520action.value%25253B%25250A%252520%252520%252520%252520%25257D%25250A%25250A%252520%252520%252520%252520case%252520%252522add_number%252522%25253A%252520%25257B%25250A%252520%252520%252520%252520%252520%252520return%252520%25257B%25250A%252520%252520%252520%252520%252520%252520%252520%252520...state%25252C%25250A%252520%252520%252520%252520%252520%252520%252520%252520number%25253A%252520action.value%252520%25252B%252520state.number%25252C%25250A%252520%252520%252520%252520%252520%252520%25257D%25253B%25250A%252520%252520%252520%252520%25257D%25250A%252520%252520%25257D%25250A%25257D%25253B&amp;display_name=Carbon&amp;url=https%3A%2F%2Fcarbon.now.sh%2F%3Fbg%3Drgba%252528171%25252C%2B184%25252C%2B195%25252C%2B1%252529%26t%3Dseti%26wt%3Dnone%26l%3Djavascript%26ds%3Dtrue%26dsyoff%3D20px%26dsblur%3D68px%26wc%3Dtrue%26wa%3Dtrue%26pv%3D56px%26ph%3D56px%26ln%3Dfalse%26fl%3D1%26fm%3DHack%26fs%3D14px%26lh%3D133%252525%26si%3Dfalse%26es%3D2x%26wm%3Dfalse%26code%3Dexport%25252520const%25252520initialState%25252520%2525253D%25252520%2525257B%2525250A%25252520%25252520number%2525253A%252525200%2525252C%2525250A%2525257D%2525253B%2525250A%2525250Aexport%25252520const%25252520AppReducer%25252520%2525253D%25252520%252528state%2525252C%25252520action%252529%25252520%2525253D%2525253E%25252520%2525257B%2525250A%25252520%25252520switch%25252520%252528action.type%252529%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520case%25252520%25252522init_stored%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520return%25252520action.value%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%2525250A%25252520%25252520%25252520%25252520case%25252520%25252522add_number%25252522%2525253A%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520return%25252520%2525257B%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520...state%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%25252520%25252520number%2525253A%25252520action.value%25252520%2525252B%25252520state.number%2525252C%2525250A%25252520%25252520%25252520%25252520%25252520%25252520%2525257D%2525253B%2525250A%25252520%25252520%25252520%25252520%2525257D%2525250A%25252520%25252520%2525257D%2525250A%2525257D%2525253B&amp;image=https%3A%2F%2Fcarbon.now.sh%2Fstatic%2Fbrand%2Fbanner.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;scroll=auto&amp;schema=carbon" width="1024" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6130a52fcdf909d20647f37d2c4cf4c/href">https://medium.com/media/d6130a52fcdf909d20647f37d2c4cf4c/href</a></iframe><p>Done! You can now use and update a shared state everywhere in your app and it will not be lost on reload :)</p><p>If there are any errors/incoherences or if you want me to write about another topic let me know!</p><p>As this is my first publication any kind of feedback is welcome!</p><h3>Sources:</h3><ul><li>For the context: <a href="https://www.netlify.com/blog/2020/12/01/using-react-context-for-state-management-in-next.js/">https://www.netlify.com/blog/2020/12/01/using-react-context-for-state-management-in-next.js/</a></li><li>Image from <a href="https://unsplash.com/">Unsplash</a></li><li><a href="https://carbon.now.sh/">Carbon</a> for the code embedding</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc7bc925d3f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/how-to-use-context-usereducer-and-localstorage-in-next-js-cc7bc925d3f2">How to use Context, useReducer and LocalStorage in Next JS</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>