<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nayib</title>
    <description>The latest articles on Forem by Nayib (@anayib).</description>
    <link>https://forem.com/anayib</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%2F396121%2F4c2726f4-9a10-49ec-987b-15423a83edc1.jpg</url>
      <title>Forem: Nayib</title>
      <link>https://forem.com/anayib</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anayib"/>
    <language>en</language>
    <item>
      <title>PACER: Structured process to solve programming problems</title>
      <dc:creator>Nayib</dc:creator>
      <pubDate>Thu, 14 Apr 2022 20:42:25 +0000</pubDate>
      <link>https://forem.com/anayib/pacer-structured-process-to-solve-programming-problems-3j7j</link>
      <guid>https://forem.com/anayib/pacer-structured-process-to-solve-programming-problems-3j7j</guid>
      <description>&lt;p&gt;When you are new to programming, you face different challenges that, if simplified,  could be reduced to three layers: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logic layer&lt;/li&gt;
&lt;li&gt;Syntax layer&lt;/li&gt;
&lt;li&gt;Productive layer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;logic layer&lt;/strong&gt; refers to your capability and skills to understand a programming problem and suggest one or more ways to solve it. The &lt;strong&gt;syntax layer&lt;/strong&gt; refers to your ability to write code in a given language, using its syntax and specifications, and the &lt;strong&gt;productive layer&lt;/strong&gt; points to the set of tools -frameworks, libraries, technologies...- and best practices, technical and soft,  that you have worked with and have experience in, so that you can be productive in a given job. &lt;/p&gt;

&lt;p&gt;I'm going to share with you a process I have named &lt;strong&gt;PACER&lt;/strong&gt; (&lt;em&gt;Problem - Algorithm - Code - Execution - Refactor&lt;/em&gt;), that will help you &lt;strong&gt;create a mental model to solve algorithmic problems in a structured and deliberate way&lt;/strong&gt;, so that you improve your logic layer skills. &lt;/p&gt;

&lt;p&gt;This process is the result of studying and testing different approaches to solve programming logic problems for over 10 years. &lt;/p&gt;

&lt;h2&gt;
  
  
  The PACER Process
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Define the problem in your own words: This helps you go through a cognitive process to validate that you understand what needs to get solved. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Input/Output&lt;/strong&gt;: Identify the expected input and output data types. This step allows you to state what data types/structures you get as an input and what data types/structures you need to return from your solution. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Examples&lt;/strong&gt;: Write some examples of the expected results based on examples of the input data. Use a desk check (manual computing) to understand what output you should get from a solution, given a specific input. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge cases&lt;/strong&gt;: Ask or suggest (when not in an interview) the edge cases that the solution should cover. This allows you to identify cases you might cover that are not explicitly shown in the problem statement. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Restrictions&lt;/strong&gt;: Write down the restrictions/rules that are given to the problem in order to set the boundaries of your solution.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
PROBLEM

Write a function that shortens a string of characters fro a - z as follows:
  If the same character repeats consecutively twice, eliminate it from the string.
  Return the shortened string.

- Input: string
- Output: string
- Examples:
  "aaabccddd" -&amp;gt; "abd"
- Edge cases: 
   * What should I return  if I get an empty string
   * What should I return if I get a data type or structure different from a String
- Restrictions: 
   The string characters are a-z
*/&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Algorithm
&lt;/h3&gt;

&lt;p&gt;In English, write down the step by step you plan to follow so that you transform the input data into the expected output.&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="cm"&gt;/*
ALGORITHM

Option 1

Define a function superReducedString( ) that receives a string  - str - as a parameter 
  declare a variable newString and assign an empty string as its value
  iterate over every character of the str
    if the character is different from the last character of newString, add it to newString
    otherwise eliminate the last character from newString
  return newString

Option 2   - Regex + Recursion - 

Define a function  superReducedString( ) that receives a string  - str -  as a parameter
 declare a regular expression that matches two equal consecutive characters
 call the string match method to see if there is still duplicates
   if null and str length is 0, return "Empty String"
   if null return str
   otherwise, call the replace method to replace/eliminate the two equal consecutive 
   characters and pass the returned value as the string parameter to call the function 
    recursively
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;p&gt;Translate the steps of the previous algorithm into a programming language of your choice.&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="c1"&gt;// CODE&lt;/span&gt;

&lt;span class="c1"&gt;// Code for algorithm - option 1 - &lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Empty String&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Code for algorithm - option 2 -  // Using regex and recursion&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;RegExp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;([&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;])\1&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;g&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Empty String&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Execution
&lt;/h3&gt;

&lt;p&gt;Execute the code to validate that the test cases are returning the expected result. &lt;/p&gt;

&lt;p&gt;You are normally given the test cases as part of the problem. In our example, the following are the test cases and their corresponding expected result:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aaabccddd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "abc"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cccxllyyy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "cxy"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "Empty String"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;baab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "Empty String"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fghiiijkllmnnno&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "fghijkmno"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chklssstt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "chkls"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Refactor
&lt;/h3&gt;

&lt;p&gt;Think about the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How could I make this code more readable? &lt;/li&gt;
&lt;li&gt;How could I make this code more efficient in terms of time complexity (takes less iterations/"time" - and space complexity - uses less memory -  &lt;sup id="1"&gt;Big O Notation&lt;/sup&gt; )?&lt;/li&gt;
&lt;li&gt;Think about edge cases that you might not have covered yet and apply the PACER process from the Algorithm step to change what is necessary to cover the new cases.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
REFACTOR
This example shortens the lines of code written
*/&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;([&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;])\1&lt;/span&gt;&lt;span class="sr"&gt;/g&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;superReducedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Empty String&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aaabccddd&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "abc"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cccxllyyy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "cxy"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;aa&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "Empty String"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;baab&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "Empty String"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fghiiijkllmnnno&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "fghijkmno"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superReducedString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chklssstt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// "chkls"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;You can &lt;a href="https://github.com/anayib/problem-solving-skills/blob/master/super_reduced_string.js"&gt;check out the complete example here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://makeitreal.s3.amazonaws.com/videos/85617582069/2021-12-18/H_wX1bSHx.mp4"&gt;watch this video with the process applied in real time - Audio in Spanish - &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want a simpler problem, feel free to pick one form the repositories below or &lt;a href="https://github.com/anayib/proceso-solucion-problemas/blob/main/ejemplos/sameString.js"&gt;follow through this one - in Spanish - &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Open repositories with examples
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/anayib/problem-solving-skills"&gt;Check out the PACER process repo in English&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/anayib/proceso-solucion-problemas"&gt;Check out the PACER process repo in Spanish&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Problems you are going to avoid with PACER
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Start writing code without really understanding the problem you have to solve.&lt;/li&gt;
&lt;li&gt;Write a solution to a problem without considering alternative solutions.&lt;/li&gt;
&lt;li&gt;Waste time writing a solution that does not consider edge cases.&lt;/li&gt;
&lt;li&gt;Writing invalid or incomplete solutions to algorithmic problems. &lt;/li&gt;
&lt;li&gt;Writing solutions that return the wrong data type or data structure. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Platforms you can use to practice your programming logic skills
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/"&gt;LeetCode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://edabit.com/challenges"&gt;Edabit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.hackerrank.com/"&gt;Hacker Rank&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any doubts, &lt;a href="https://www.linkedin.com/in/nayibabdala/"&gt;send me a direct message via LinkedIn&lt;/a&gt; and I will get back to you ASAP. &lt;/p&gt;




&lt;p&gt;↩&lt;small&gt;&lt;span id="ft1"&gt; Big O notation: It’s a way to describe how the runtime of an algorithm grows as its inputs grow. In other words, it is a way to describe how the number of operations to execute an algorithm increases as the input of data to the algorithm increases. Some resources you may want to check out &lt;a href="https://www.101computing.net/big-o-notation/"&gt;Big O Notation 101&lt;/a&gt;, &lt;a href="https://rithmschool.github.io/function-timer-demo/"&gt;Big O notation calculator&lt;/a&gt; &lt;/span&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Note: The programming problem used in this blog post to showcase the PACER process  was taken from the &lt;a href="https://edabit.com/challenges"&gt;Edabit&lt;/a&gt; &lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>React Context and Hooks: An Open Source Project to Understand How They Work</title>
      <dc:creator>Nayib</dc:creator>
      <pubDate>Tue, 07 Jul 2020 22:52:10 +0000</pubDate>
      <link>https://forem.com/anayib/react-context-and-hooks-an-open-source-project-to-understand-how-they-work-2h76</link>
      <guid>https://forem.com/anayib/react-context-and-hooks-an-open-source-project-to-understand-how-they-work-2h76</guid>
      <description>&lt;p&gt;&lt;em&gt;Intermediate level article&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There are different approaches regarding the best ways to learn something new, and one of them is by doing. I agree with that approach, as long as the basics are already clear, and you have a general mental model that gives you the right context about what you are learning.&lt;/p&gt;

&lt;p&gt;For instance, if you are going to learn how to use &lt;em&gt;Context&lt;/em&gt; and &lt;em&gt;Hooks&lt;/em&gt; from the React API, you already need to be familiar with the following topics, otherwise you will be totally lost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functional components&lt;/li&gt;
&lt;li&gt;React Life-cycle Events&lt;/li&gt;
&lt;li&gt;The concept of State and State Management in JavaScript&lt;/li&gt;
&lt;li&gt;The concept of a Hook&lt;/li&gt;
&lt;li&gt;Context and Scope JavaScript concepts&lt;/li&gt;
&lt;li&gt;The DOM&lt;/li&gt;
&lt;li&gt;JavaScript modern features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you feel comfortable with the above topics, keep reading; otherwise, you can always come back to this later.&lt;/p&gt;

&lt;p&gt;This time, I want to share with you my experience building a React App from the ground up using the &lt;em&gt;Context&lt;/em&gt; React object and React &lt;em&gt;Hooks&lt;/em&gt;, no &lt;em&gt;Class Components&lt;/em&gt; included, just &lt;em&gt;Functional Components&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Project
&lt;/h2&gt;

&lt;p&gt;A simple blog with a React App in the front end that allows you to search and read blog articles (built with the &lt;em&gt;Context&lt;/em&gt; and &lt;em&gt;Hooks&lt;/em&gt; React features). The articles are retrieved from a back end application built in NodeJS, fetching the data via API calls.&lt;/p&gt;

&lt;p&gt;You can find the &lt;a href="https://github.com/anayib/react-node-blog-app"&gt;open source project here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Objective
&lt;/h2&gt;

&lt;p&gt;My objective with this project is to create a simple web app that serves as a reference for those having trouble grasping the concepts and practical aspects of using the React &lt;em&gt;Context&lt;/em&gt; object and &lt;em&gt;hooks&lt;/em&gt; to build React Apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  The App Architecture
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Front End
&lt;/h3&gt;

&lt;p&gt;The front end is a React App built using &lt;em&gt;Context&lt;/em&gt;, &lt;em&gt;Hooks&lt;/em&gt; and &lt;em&gt;Functional Components&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Remember that a &lt;em&gt;Context&lt;/em&gt; object is a JavaScript object that allows you to manage the state (data) of your application. In this project, we have a &lt;em&gt;Context&lt;/em&gt; object that helps us handle the article's data fetched from the back end (&lt;a href="https://github.com/anayib/react-node-blog-app/blob/master/client/Context.js"&gt;Context.js&lt;/a&gt;) and another &lt;em&gt;Context&lt;/em&gt; that helps us handle the articles that should be available to some components in order to be shown to the user after a search has been requested (&lt;a href="https://github.com/anayib/react-node-blog-app/blob/master/client/SearchContext.js"&gt;SearchContext.js&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  The Back End
&lt;/h3&gt;

&lt;p&gt;The back end is built with NodeJS and Express. Its only purpose is to make an end-point available to serve the articles data in JSON format when requested from a client, in our case, from the React App.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Data
&lt;/h3&gt;

&lt;p&gt;For this version, I did not include any database, but I used the file system to save the articles. Why? Because the focus of this project is mainly the front end, and this approach to store data is good enough to make our NodeJS API work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Context and Hooks
&lt;/h2&gt;

&lt;p&gt;There are pros and cons regarding the use of these React API new features. Nevertheless, here are the ones I found the most relevant during this project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Pros: Using &lt;em&gt;Context&lt;/em&gt; allows you to pass data to any component in your app without having to pass it manually every level down the DOM tree. For this specific project, the &lt;em&gt;Context&lt;/em&gt; feature allowed me to manage the state of the blog posts in a single component (the context provider) that could be imported in any other component, in order to give it access to the data that has been previously retrived from the back end via an API call.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cons: Right now, it is harder to test components that use data from the &lt;em&gt;Context&lt;/em&gt; providers when using &lt;em&gt;Jest&lt;/em&gt; than testing them the traditional way. The other aspect is that using &lt;em&gt;Hooks&lt;/em&gt; makes it "more magical" when managing the state of your application data than when you are using the tradition life cycle methods from a &lt;em&gt;Class Component&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  React Hooks vs Traditional Life Cycle Methods
&lt;/h2&gt;

&lt;p&gt;I assume you are familiar with the &lt;code&gt;componentDidMount&lt;/code&gt;, &lt;code&gt;componentDidUpdate&lt;/code&gt;, and the other life cycle methods of React. In brief, and being simplistic for learning purposes, some of the &lt;em&gt;Hooks&lt;/em&gt; allow you to do the same as the life cycle methods, but from within &lt;em&gt;Functional Components&lt;/em&gt;, there is no need to write a &lt;em&gt;Class Component&lt;/em&gt; to initialize and handle the state of the component.&lt;/p&gt;

&lt;p&gt;Let's see an example from the project using the &lt;em&gt;useState()&lt;/em&gt; and &lt;em&gt;useEffect&lt;/em&gt; React &lt;em&gt;Hooks&lt;/em&gt;. Check the following code, including the commented code which explains what every line is written for:&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="c1"&gt;// Context.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// imports React, and the useState and useEffect basic hooks from react library&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// imports axios from the axios package to make the API call to the back-end&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// creates a Context object from the React.createContext() method. You will reference this Context object when the blog posts data fetched from the NodeJS API needs to be accessible by other components at different nesting levels.&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="c1"&gt;// Functional component definition for a component named ContextProvider. This Functional Component will be in charged of fetching the data from the back end and handle the state (blog articles) data of the application&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// export the ContextProvider functional component, and the Context object to make them available to other modules in the React app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the previous code, we have created a file &lt;em&gt;Context.js&lt;/em&gt; whose only responsability will be to give to other components access to the articles' data, which is retrieved from the back end. To do so, we need to create a new &lt;em&gt;Context&lt;/em&gt; (&lt;code&gt;const Context = React.createContext()&lt;/code&gt;), and a &lt;em&gt;Functional Component&lt;/em&gt; that allows us to provide that &lt;em&gt;Context&lt;/em&gt; to other components (&lt;code&gt;function ContextProvider( ) {}&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;Now that we have the basic structure of our file to handle the articles' state using our own &lt;em&gt;Context&lt;/em&gt;, let's write the code inside the &lt;em&gt;ContextProvider&lt;/em&gt; &lt;em&gt;Functional Component&lt;/em&gt;, which will set the initial state and handle any changes:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setArticles&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt; &lt;span class="c1"&gt;// useState() hook call, that initializes the state of the articles to an empty array&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// useEffect hook call which will be invoked the first time the DOM mount. it is like using componentDidMount in Class Components&lt;/span&gt;
    &lt;span class="nx"&gt;fetchArticles&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// the function that will be called as soon as the DOM mounted&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchArticles&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// the asyncronous definition of the fetchArticles function that will retrieve the articles from the NodeJS api&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/api/tutorials&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// the API call to fetch the articles from the back end&lt;/span&gt;
      &lt;span class="nx"&gt;setArticles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// the setArticles function allows us to update the state of the component via the useState() hook&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="nx"&gt;articles&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Context.Provider&amp;gt;; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;returned&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ContextProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a closer look at every line written above.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ContextProvider Component
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;function ContextProvider({ children }) {...}&lt;/code&gt; : This is the &lt;em&gt;Functional Component&lt;/em&gt; definition that accepts a parameter called &lt;em&gt;children&lt;/em&gt;. The &lt;em&gt;children&lt;/em&gt; parameter is any &lt;em&gt;Functional Component&lt;/em&gt; that will be receiving the state being handled by this &lt;em&gt;ContextProvider&lt;/em&gt; function, and are children components of the &lt;em&gt;ContextProvider&lt;/em&gt; component. &lt;a href="https://github.com/anayib/nayib-personal-blog-/blob/d1e483972213f08c5acb49167c76c5cb7931324a/client/index.js#L9"&gt;Check out this example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The curly braces included in &lt;code&gt;{children}&lt;/code&gt; , may appear strange to you. This is the way the new JavaScript features allow us to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;deconstruct an object or array&lt;/a&gt;. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nayib&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Abdalá&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// JS object deconstruction&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Nayib&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Abdalá&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In brief, the &lt;code&gt;const [articles, setArticles] = useState([]);&lt;/code&gt; line helped us initialize and handle the state of the articles that will be fetched from the back end. Let's see how.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Initialization of the App State with the useState() Hook
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;const [articles, setArticles] = useState([]);&lt;/code&gt;: Does this line look strange to you? It is simple. The &lt;code&gt;const&lt;/code&gt; keyword allows us to declare a constant called &lt;code&gt;articles&lt;/code&gt; , and one called &lt;code&gt;setArticles&lt;/code&gt;. The values assigned to each of these constants are the returned values from calling the &lt;code&gt;useState()&lt;/code&gt; hook, which returns an array with 2 elements, and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;deconstruct JavaScript&lt;/a&gt; feature allows us to assign each of those elements to each constant we have defined on the left side of the expression &lt;code&gt;const [articles, setArticles] = useState([]);&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The array returned by the &lt;code&gt;useState()&lt;/code&gt; hook is an array containing the current state for a given variable, and a function that updates that state and can be used at any time in your &lt;em&gt;Functional Component&lt;/em&gt; in order to update that state. In this case, we are initializing the value of &lt;code&gt;articles&lt;/code&gt; to an empty array (when passing &lt;code&gt;[]&lt;/code&gt; to the &lt;code&gt;useState([])&lt;/code&gt; function).&lt;/p&gt;

&lt;p&gt;You can learn more about the &lt;a href="https://reactjs.org/docs/hooks-reference.html#usestate"&gt;useState() hook here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Listening for State Changes with the useEffect() Hook
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useEffect(() =&amp;gt; { ... }, [])&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useEffect()&lt;/code&gt; hook will run after every completed render, but you can set it to only run if a certain value has changed. &lt;code&gt;useEffect()&lt;/code&gt; receives two parameters: a function, and the second argument is the configuration of when the first parameter function should be called.&lt;/p&gt;

&lt;p&gt;If you pass an empty array as a second parameter, the function should be called only the first time the complete render happens. If you pass one or more variables names as elements of the array passed as the second argument to &lt;code&gt;useEffect()&lt;/code&gt;, every time there is a change in the value of any of those variables, the function passed as a first argument to &lt;code&gt;useEffect()&lt;/code&gt; will be called.&lt;/p&gt;

&lt;p&gt;In our case, the function passed as a first argument to &lt;code&gt;useEffect()&lt;/code&gt; , will be called only the first time the DOM renders, as we are passing an empty array as a second argument to &lt;code&gt;useEffect(() =&amp;gt; { ... }, [])&lt;/code&gt;. You can learn more about the &lt;a href="https://reactjs.org/docs/hooks-reference.html#useeffect"&gt;useEffect() hook here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Every time the &lt;code&gt;useEffect(() =&amp;gt; { ... }, [])&lt;/code&gt; hook is called, the &lt;code&gt;fetchArticles()&lt;/code&gt; function will be called, which will fetch the articles' data from the back end NodeJS API of &lt;a href="https://github.com/anayib/react-node-blog-app"&gt;this project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once the &lt;code&gt;fetchArticles()&lt;/code&gt; is called, the program in the body of this fuction will call the &lt;code&gt;setArticles(content.data);&lt;/code&gt; function, which receives, as an argument, the &lt;code&gt;content.data&lt;/code&gt; data fetched from the API, and will set the returned value from &lt;code&gt;content.date&lt;/code&gt; as the updated value of &lt;code&gt;articles&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how the &lt;code&gt;useEffect()&lt;/code&gt; hook allow us to listen to new renders of the DOM, and execute an action once or every time there is a change in the mounted DOM, or any specific variable that we want to pass to the &lt;code&gt;useEffect()&lt;/code&gt; hook as a second argument.&lt;/p&gt;

&lt;h3&gt;
  
  
  Returning the Context Provider that will Give Access to the State to Other Components
&lt;/h3&gt;

&lt;p&gt;Once we have a clear understanding of how to handle the state of our articles, we now need to return what is required so that we can make the &lt;code&gt;articles&lt;/code&gt; state available to other components. To do so, we need to have access to our &lt;em&gt;Provider&lt;/em&gt; React component, so that we can share the data that is initialized and handled in the &lt;code&gt;ContextProvider&lt;/code&gt; component with other components.&lt;/p&gt;

&lt;p&gt;Every React &lt;em&gt;Context&lt;/em&gt; object has two components as methods when creating it by using the React API &lt;code&gt;React.createContext()&lt;/code&gt; function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;Provider&lt;/em&gt; method - A component that provides the value&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;Consumer&lt;/em&gt; method - A component that is consuming the value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;em&gt;Provider&lt;/em&gt; React component allows children components to consume any data the &lt;em&gt;Provider&lt;/em&gt; has access to.&lt;/p&gt;

&lt;p&gt;The way you make the state of the &lt;code&gt;ContextProvider&lt;/code&gt; component available is by returning a &lt;code&gt;Context.Provider&lt;/code&gt; React component, and passing a &lt;code&gt;value&lt;/code&gt; prop containing the &lt;code&gt;articles&lt;/code&gt; data, in order to make it available to any consuming components that are decendants of this &lt;em&gt;Provider&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What?! I know, it seems confusing, but it is actually simple. Let's go trough the code in chunks to make it clearer:&lt;/p&gt;

&lt;p&gt;When calling the &lt;code&gt;&amp;lt;Context.Provider /&amp;gt;&lt;/code&gt; component, and passing the variables you include in the &lt;code&gt;value&lt;/code&gt; props to that &lt;em&gt;Provider&lt;/em&gt; component, which in our case is the &lt;code&gt;articles&lt;/code&gt; variable, you will give any descendant component that might be wrapped by the &lt;em&gt;Provider&lt;/em&gt; access to that variable.&lt;/p&gt;

&lt;p&gt;If we log the &lt;code&gt;&amp;lt;Context.Provider /&amp;gt;&lt;/code&gt; component to the console for our project example, you will see the following:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;expand&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
    &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;articles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="nx"&gt;Nodes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;wrapper&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not get scared about the details; what you see above is basically the &lt;em&gt;Provider&lt;/em&gt; component which has access to the data you have given access to via the &lt;code&gt;value&lt;/code&gt; prop.&lt;/p&gt;

&lt;p&gt;To sum it up, you need to return a &lt;em&gt;Provider&lt;/em&gt; component from your &lt;em&gt;ContextProvider&lt;/em&gt; component, with the data that you need to make available to other &lt;code&gt;children&lt;/code&gt; components: &lt;code&gt;return &amp;lt;Context.Provider value={{ articles }}&amp;gt;{children}&amp;lt;/Context.Provider&amp;gt;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For instance, all the components wrapped in the &lt;code&gt;&amp;lt;ContextProvider /&amp;gt;&lt;/code&gt; component below, will have access to the &lt;em&gt;Context&lt;/em&gt; data (&lt;a href="https://github.com/anayib/react-node-blog-app/blob/master/client/index.js"&gt;check out the file in the repo&lt;/a&gt;) :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ContextProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="cm"&gt;/* all the children components called here will have access to the data from
  the ContextProvider component */&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ContextProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the above is overwhleming, don't worry. Read it again. The take-away is that you need to wrap all the &lt;code&gt;children&lt;/code&gt; elements that will need access to the data from your &lt;em&gt;Provider&lt;/em&gt; in the &lt;em&gt;Context.Provider&lt;/em&gt; component.&lt;/p&gt;

&lt;p&gt;Take a break...&lt;/p&gt;

&lt;p&gt;The next section is similar to this one, but it explains the &lt;code&gt;&amp;lt;ContextProviderSearch /&amp;gt;&lt;/code&gt; component I created to handle the data of a given search.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Use of Context as a Way to Separate Concerns and Handle Data
&lt;/h2&gt;

&lt;p&gt;As a separate concern in our application, we will need a new &lt;em&gt;Context&lt;/em&gt; that handles the state of the &lt;code&gt;articles&lt;/code&gt; that should be shown to the user when a given search query takes place.&lt;/p&gt;

&lt;p&gt;I have called this new &lt;em&gt;Context&lt;/em&gt; the &lt;em&gt;ContextProviderSearch&lt;/em&gt;. It depends on the &lt;code&gt;articles&lt;/code&gt; data from the &lt;code&gt;Context.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's take a look at the &lt;a href="https://github.com/anayib/react-node-blog-app/blob/master/client/SearchContext.js"&gt;SearchContext.js&lt;/a&gt; file to understand how the &lt;em&gt;Context&lt;/em&gt; object from the previous section is used to access the &lt;code&gt;articles&lt;/code&gt; in this case:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// code omitted&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;AppContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Context&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// imports the Context provided by Context.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// code omitted&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ContextProviderSearch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// code omitted&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;articles&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Access the articles array from the Context.js file&lt;/span&gt;

  &lt;span class="c1"&gt;// code omitted&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt;
      &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="cm"&gt;/*all the props that will be required by consumer components*/&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* any consumer component*/&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Context.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ContextProviderSearch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most important lines of this file for our purpose are &lt;code&gt;import { Context as AppContext } from "./Context"&lt;/code&gt; and &lt;code&gt;const { articles } = useContext(AppContext)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;import { Context as AppContext } from "./Context"&lt;/code&gt; helps us import the context from our &lt;code&gt;Context,js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;const { articles } = useContext(AppContext)&lt;/code&gt; expression uses the &lt;code&gt;useContext()&lt;/code&gt; React hook, which accepts the &lt;code&gt;AppContext&lt;/code&gt; as an argument, and returns the current context value we imported from &lt;code&gt;Context.js&lt;/code&gt;. Using the deconstruct JavaScript feature, we create a constant with the &lt;code&gt;articles&lt;/code&gt; array, to which the &lt;code&gt;AppContext&lt;/code&gt; has access to.&lt;/p&gt;

&lt;p&gt;This way, our &lt;code&gt;ContextProviderSearch&lt;/code&gt; now has access to the &lt;em&gt;Context&lt;/em&gt; from &lt;code&gt;Context.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In brief, you can use the &lt;code&gt;useContext&lt;/code&gt; React hook to have access to any &lt;em&gt;Context&lt;/em&gt; you have created in your application in order to access the state that the given &lt;em&gt;Context&lt;/em&gt; manage.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/anayib/react-node-blog-app/blob/master/client/SearchContext.js"&gt;SearchContext.js&lt;/a&gt; file includes some logic that is out of the scope of this article. If you have any questions about it, just &lt;a href="http://www.nayibabdala.com/contact"&gt;ask me&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to be Improved in This Project
&lt;/h2&gt;

&lt;p&gt;I created this project with an educational objective. There are several things that could be improved. I'm going to list some of them below, in case you are curious or have alrady identified them while checking the repo:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing: Additional unit tests should be added to check that the contexts data management is well. Also, adding tests to the back end NodeJS API would be a good idea.&lt;/li&gt;
&lt;li&gt;Data Storage: For educational purposes, it is ok to store the articles in the file system. Nevertheless, it would be a better idea to integrate an SQL or NoSQL database to the project. Some options are Posgres with Squelize as a ORM, or MongoDB with Mongoose as a DRM.&lt;/li&gt;
&lt;li&gt;Browser data Storage: The &lt;code&gt;articles&lt;/code&gt; data is temporarly stored in the &lt;code&gt;Window.localStorage&lt;/code&gt; storage object once it is fetched from the &lt;code&gt;Context.js&lt;/code&gt; via the NodeJS API. The &lt;code&gt;Window.localStorage&lt;/code&gt; has a storage size limit that might be not enough when handling several articles.&lt;/li&gt;
&lt;li&gt;Lazy load: You could add the &lt;a href="https://webpack.js.org/guides/lazy-loading/"&gt;Lazy Loading&lt;/a&gt; utility to improve the size of the files created by webpack.&lt;/li&gt;
&lt;li&gt;Add API authentication&lt;/li&gt;
&lt;li&gt;Implement error boundaries&lt;/li&gt;
&lt;li&gt;Implement type-checking for the React application&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are not familiar with the concepts from the list above, check them out and try to implement them by cloning the repository. The exercise will strengthen your React skills.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Repository
&lt;/h2&gt;

&lt;p&gt;You can find the &lt;a href="https://github.com/anayib/react-node-blog-app"&gt;Open-Source project here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this article and project will serve as a reference for you to understand how to use &lt;em&gt;Context&lt;/em&gt; and &lt;em&gt;Hooks&lt;/em&gt; in your React apps.&lt;/p&gt;

&lt;p&gt;Article originally posted at &lt;a href="http://www.nayibabdala.com"&gt;www.nayibabdala.com&lt;/a&gt;&lt;/p&gt;

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