<?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: CinArb/</title>
    <description>The latest articles on Forem by CinArb/ (@cinarb2).</description>
    <link>https://forem.com/cinarb2</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%2F737544%2F6a09acf6-57fd-46cf-b992-8639143aa754.jpg</url>
      <title>Forem: CinArb/</title>
      <link>https://forem.com/cinarb2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cinarb2"/>
    <language>en</language>
    <item>
      <title>7 React optimization techniques you should know</title>
      <dc:creator>CinArb/</dc:creator>
      <pubDate>Sun, 01 May 2022 00:21:05 +0000</pubDate>
      <link>https://forem.com/cinarb2/7-react-optimization-techniques-you-should-know-490c</link>
      <guid>https://forem.com/cinarb2/7-react-optimization-techniques-you-should-know-490c</guid>
      <description>&lt;p&gt;Although the performance of an app in React depends on things like the browser running the code, the browser version, the device where our application is running, the latency of the network that the user has, etc. In this article, I want to share with you what we can do to improve performance from the React.js side.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Use of performance measurement tools:
&lt;/h2&gt;

&lt;p&gt;First we must check our app for possible bottlenecks and errors, and in order to do so we can take advantage of some free tools that will help us greatly: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sentry:&lt;br&gt;
Sentry is presented as an "Error Tracking Software" and will allow your team to quickly find and fix bugs. If you want to learn more about Front-End Monitoring and how to get started with Sentry, please visit this &lt;a href="https://blog.openreplay.com/front-end-monitoring-with-sentry-io-and-react"&gt;link&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Profiler React dev tools:&lt;br&gt;
As simple as installing the React Developer Tools extension in your browser of choice and inspecting an element on your page, to have the profiler and components option enabled and start measuring your application.  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The profiler tab shows us how many times our page has been rendered under a certain action, how long it has taken, and why it has done so. &lt;/p&gt;

&lt;p&gt;You are probably  going to find a component that is not acting the way you were expecting but you can now make decisions on whether it is desirable to optimize the hierarchy of your components or if child components should render when their parent component does, and so on. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Manage the execution of functions
&lt;/h2&gt;

&lt;p&gt;Let's say you have a search bar on your website that executes a function every time the user types in input, this function seeks to fetch data from an API call and displays the search results in real time.&lt;/p&gt;

&lt;p&gt;Another example we see frequently on web pages is loading new items from a list of items on the screen. A third example would be if you have a listener to resize the screen, which calculates the dimensions.&lt;/p&gt;

&lt;p&gt;If the user executes any of these actions several times, the function responsible for executing them will fire several times, and the performance of your website, or web application, will be affected.&lt;/p&gt;

&lt;p&gt;Those are perfect examples to consider libraries like lodash and its debounce() method.  A technique used to improve browser performance that allows a function to be executed only once, at the specified time-frequency since the last time the same function was called.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.Memoize react components:
&lt;/h2&gt;

&lt;p&gt;Memoise means to memorize a value to avoid processing it again, generally used to save you the cost of producing a value over and over again.&lt;/p&gt;

&lt;p&gt;Now in react we have different ways to achieve the above, by means of the following techniques:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React.memo(): is simply a higher order component (HOC) that wraps the component we want to store in memory. So if for example its parent component is rendered, react.Memo will validate if its properties have changed, and if not, it will not render it again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method should be used with great caution, as this comparison process can be more costly than rendering the component we are trying to avoid rendering. &lt;/p&gt;

&lt;p&gt;Memo is ideal for components that:&lt;/p&gt;

&lt;p&gt;suffers multiple renderings with the use of the application and generally receives the same props.&lt;br&gt;
receives props that change infrequently or not at all.&lt;br&gt;
very bulky components that have a very large impact on performance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;useMemo: This function is a React hook for memoising the value returned by a function. The useMemo function accepts two arguments and returns a value. The first argument is the function and the second, like useCallback, is a variable to watch, so a new value will not be generated until that variable changes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useCallback: This hook allows us to memorize the function we pass it as an argument, always returning the same "instance" after rendering until one of the dependencies we specify changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Virtualize a long list:
&lt;/h2&gt;

&lt;p&gt;When we talk about virtualizing a list or grid, we refer to rendering on screen only the elements that the user is currently viewing. &lt;/p&gt;

&lt;p&gt;Virtualization in lists can be both horizontal and vertical (depending on the direction we indicate to the list) and for grids, the virtualization is both horizontal and vertical at the same time. To achieve virtualization, windowing techniques are used to calculate which elements should be displayed and which should not.&lt;/p&gt;

&lt;p&gt;There are also several libraries that allow us to create virtualized lists but there are 2 that stand out from the rest: &lt;a href="https://github.com/bvaughn/react-virtualized"&gt;react-virtualized&lt;/a&gt; and &lt;a href="https://github.com/bvaughn/react-window"&gt;react-window&lt;/a&gt;. Both libraries are from Brian Vaughn who is one of the developers of the React team. &lt;/p&gt;

&lt;h2&gt;
  
  
  5. Code splitting React components:
&lt;/h2&gt;

&lt;p&gt;Sometimes the final bundle of our application can have  considerable weight and, in order to "see something" on the screen, users have to download all the information and this takes time.&lt;/p&gt;

&lt;p&gt;This is where different code splitting techniques come to our aid.&lt;/p&gt;

&lt;p&gt;As per MDN,&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Code splitting is the splitting of code into various bundles or components which can then be loaded on demand or in parallel.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this way, we ensure that when a page initially loads, a smaller bundle will be retrieved from the server. Using the dynamic import syntax, React.lazy, and Suspense, we can code split a React component.&lt;/p&gt;

&lt;p&gt;There are also React code split libraries that can do this . Loadable-component are one of those.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Dependency optimization:
&lt;/h2&gt;

&lt;p&gt;If you are using some development dependencies in your application, it is important to check how much functionality you are using out of the large amount that is included by default. &lt;/p&gt;

&lt;p&gt;This is where we can use tools like webpack bundle analyzer plugin that displays an interactive graph with the result of our bundle, what dependencies it contains and how much they weigh. &lt;/p&gt;

&lt;p&gt;If after the analysis you find that you only used a small amount of css styles from the TailwindCSS framework, use tools like PurgeCss to help minimize the amount of css files in your final bundle. &lt;/p&gt;

&lt;p&gt;Take a look at this list of tips on how to &lt;a href="https://github.com/GoogleChromeLabs/webpack-libs-optimizations"&gt;optimize your libraries with webpack&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  7. Enable Gzip compression on web server:
&lt;/h2&gt;

&lt;p&gt;GZIP is a software application used to compress and decompress files.  GZIP's function is to compress files before they are served to the client browser. It allows further reduction of your HTML, CSS, and JavaScript files. Some have experienced up to 70% reduction due to compression.&lt;/p&gt;

&lt;p&gt;If your backend is built on node.js and express you can use the compression middleware in the main file of your Node.js application. This will enable GZIP, which supports different compression schemes. &lt;/p&gt;

&lt;p&gt;In addition to the default settings, you can customize your compression to suit your needs. There are several different properties you can use in the options object. For a complete list of properties, you can choose from, see the documentation &lt;a href="https://www.npmjs.com/package/compression"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;By applying certain techniques to a react application we can greatly improve performance. Many of these techniques or tips do not require sophisticated software but are free.&lt;/p&gt;

&lt;p&gt;Although this was a small introduction to the world of web performance, don't hesitate to keep digging further into the subject and find more technical information on how to apply each of the techniques mentioned in this article. &lt;/p&gt;

&lt;p&gt;Here I will leave you a list of interesting articles and if you want to contribute or help to improve this article feel free to leave a comment. &lt;/p&gt;

&lt;p&gt;Sentry: &lt;a href="https://blog.openreplay.com/front-end-monitoring-with-sentry-io-and-react"&gt;https://blog.openreplay.com/front-end-monitoring-with-sentry-io-and-react&lt;/a&gt; &lt;br&gt;
Profiler React dev tools: &lt;a href="https://blog.openreplay.com/the-definitive-guide-to-profiling-react-applications"&gt;https://blog.openreplay.com/the-definitive-guide-to-profiling-react-applications&lt;/a&gt; &lt;br&gt;
Debounce: &lt;a href="https://www.freecodecamp.org/news/javascript-debounce-example/"&gt;https://www.freecodecamp.org/news/javascript-debounce-example/&lt;/a&gt; &lt;br&gt;
Memoization: &lt;a href="https://www.freecodecamp.org/news/memoization-in-javascript-and-react/"&gt;https://www.freecodecamp.org/news/memoization-in-javascript-and-react/&lt;/a&gt; &lt;br&gt;
Code splitting: &lt;a href="https://www.xenonstack.com/insights/code-splitting-in-react"&gt;https://www.xenonstack.com/insights/code-splitting-in-react &lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>performance</category>
    </item>
    <item>
      <title>Asynchronous JavaScript: callbacks, promises, Async/await, Event loop - part 2</title>
      <dc:creator>CinArb/</dc:creator>
      <pubDate>Wed, 30 Mar 2022 20:37:50 +0000</pubDate>
      <link>https://forem.com/cinarb2/asynchronous-javascript-callbacks-promises-asyncawait-event-loop-part-2-98f</link>
      <guid>https://forem.com/cinarb2/asynchronous-javascript-callbacks-promises-asyncawait-event-loop-part-2-98f</guid>
      <description>&lt;p&gt;In the first part of this topic we could see that ​​even though javascript is a single-threaded language, the environment in which it is executed helps us to work in an asynchronous way. &lt;/p&gt;

&lt;p&gt;Many elements are involved in the execution of our code and one of those elements is the Web APIs. Operations we can do with the web APIs are: Fetch data, Set local storage, DOM manipulation, etc… These actions are taken apart from the main thread while they are completed. Now the question is, How does our program know when operations have ended?  What are we going to do once these operations are finished? &lt;/p&gt;

&lt;p&gt;It’s here where javascript brought to the table different mechanisms in order to control and manage these asynchronous actions. Let's take a look.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks represent: What do you want to do once your asynchronous operation is over?
&lt;/h2&gt;

&lt;p&gt;Most asynchronous actions take as an argument another function (callbacks), and once the asynchronous task is finished, the callback function is called. &lt;/p&gt;

&lt;p&gt;To see a callback in action lets take as an example setTimeOut, which is an asynchronous function that takes a callback and the time at which it should be executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4phobu1qdge371nntrq5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4phobu1qdge371nntrq5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;‘Hello’ is printed on the console&lt;/li&gt;
&lt;li&gt;Secondly, the setTimeOut function is taken to another thread, and allows the script to continue running. (setTimeOut is a Web API, so it’s placed inside its respective container) &lt;/li&gt;
&lt;li&gt;The last line of code is executed, ‘End’ is printed on the console&lt;/li&gt;
&lt;li&gt;Event loop, check that the call stack is now empty and the callback queue has one callback in line. &lt;/li&gt;
&lt;li&gt;Then move the console.log function to the call stack and now we can see the word ‘world’ printed on the console. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important to realize that even if we configure our setTimeOut to run in 0 seconds. It’s not going to be executed immediately. The result is going to be the same as before due to the setTimeOut being run in another thread. &lt;/p&gt;

&lt;p&gt;The problem comes when you need the result of one function to call the following function. Then you start to nest as many callbacks as you need. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxqqm03e6wegor9ixvqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxqqm03e6wegor9ixvqa.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In essence, we are doing more complicated things, detracting from legibility, making the code difficult to maintain. This structure is also known as the Pyramid of Doom, or Callback hell.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promises: introduced with ECMAScript 6, and brought to the language a more concise way of working with asynchronous functions
&lt;/h2&gt;

&lt;p&gt;According to &lt;a href="https://eloquentjavascript.net/11_async.html" rel="noopener noreferrer"&gt;Eloquent Javascript&lt;/a&gt;: &lt;em&gt;“A promise is an asynchronous action that may complete at some point and produce a value. It is able to notify anyone who is interested when its value is available”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the most common uses of promises in web development is to control the flow of requests (Ajax) to web servers. Let's take a look at the following snippet. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07a7c9oq3tue30clweqr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07a7c9oq3tue30clweqr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have the Fetch method, a web API that returns a promise, and instead of passing async functions we &lt;strong&gt;attach them&lt;/strong&gt; with another method called &lt;strong&gt;then()&lt;/strong&gt;.  The result is &lt;em&gt;a more legible and maintainable code, fixing the clutter of nested callbacks and also, making error handling simpler.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One last thing that is important to mention is that with the introduction of Promises, a second high-priority queue was incorporated into the javascript runtime environment. This new queue is called the &lt;strong&gt;​​microtask queue&lt;/strong&gt;. So when a new promise event occurs, this priority queue will be serviced first. Thus, we ensure that the callbacks of the promises will be executed in the future, and as soon as possible.&lt;/p&gt;

&lt;p&gt;If you want to dig deeper on how to actually create a Promise and how to handle errors click on &lt;a href="https://javascript.info/promise-basics" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  ECMAScript 7 and the arrival of async/await
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, with the advent of promises we are able to avoid nesting callbacks and structure our code a little better. Yet javascript found a way to give us another methodology where we can still handle asynchronous tasks but drop the chaining model of then() to use one in which we work in a more traditional way.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz8wr8qe6qxcnph5stvgu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz8wr8qe6qxcnph5stvgu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First of all,  we have the keyword &lt;strong&gt;async&lt;/strong&gt; that it’s placed before the function we want to define as asynchronous&lt;/li&gt;
&lt;li&gt;Inside the function, we now can proceed to use the word &lt;strong&gt;await&lt;/strong&gt;, and as the name implies, causes the browser to pause the execution of JavaScript code until the promise implied is resolved.&lt;/li&gt;
&lt;li&gt;Optionally, in case you want to check for errors or rejected promises, you can always use try/catch blocks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that, in the end, async await is a &lt;strong&gt;syntactic sugar&lt;/strong&gt; for promises. The code looks sequential, in a synchronous way but it’s still a promise-based asynchronous code. It is also mentioned that this method enables some interesting optimizations of Memory and performance too in JS Engine.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Asynchronous JavaScript: callbacks, promises, Async/await, Event loop - part 1</title>
      <dc:creator>CinArb/</dc:creator>
      <pubDate>Tue, 22 Mar 2022 18:24:51 +0000</pubDate>
      <link>https://forem.com/cinarb2/asynchronous-javascript-callbacks-promises-asyncawait-event-loop-part-1-4ep8</link>
      <guid>https://forem.com/cinarb2/asynchronous-javascript-callbacks-promises-asyncawait-event-loop-part-1-4ep8</guid>
      <description>&lt;p&gt;When we try to understand asynchrony in javascript we usually go straight to concepts like callbacks, promises, async/await and we leave out something as important as how javascript itself works. &lt;/p&gt;

&lt;p&gt;We will therefore start from the innermost part of the program, such as the javascript runtime environment, and from there we will be uncovering layer by layer until we reach the topics mentioned above.  &lt;/p&gt;

&lt;h2&gt;
  
  
  1.Javascript is a single-threaded language.
&lt;/h2&gt;

&lt;p&gt;Javascript code is executed in order, line by line. It must finish executing a piece of code before moving on to the next. This is also known as a synchronous programming model, things happen one at a time.&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;let&lt;/span&gt; &lt;span class="nx"&gt;a&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the biggest issues with synchronous code comes when you have to call a function that requires a certain amount of time to be solved. The next operation needs to wait even if the result of the previous operation is not required. Think of this situation in big applications, the amount of time required to run a script is very long. Loading times can be slower with synchronous programming. &lt;/p&gt;

&lt;h2&gt;
  
  
  2.JavaScript code is executed in a single thread, but that doesn’t mean the whole JavaScript runtime environment works in a single thread.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4vj0DqHX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1760/1%2A5K5JL9jhX10Yt2iArGVv4w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4vj0DqHX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1760/1%2A5K5JL9jhX10Yt2iArGVv4w.gif" alt="Alt Text" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you take a look at the image above you can see a visual representation of what a javascript runtime environment is. It consists of different elements and features that help to run the javascript code.  Those elements are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Javascript&lt;/strong&gt; engine: program in charge of executing and running our javascript code, it is basically in charge of translating the code we write to machine language. In fact, there are not one if not several engines: V8 for Chrome, Chakra for Edge, Spidermonkey for Firefox. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The heap&lt;/strong&gt;: responsible for storing our data. It is used to allocate our variables and objects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The call stack&lt;/strong&gt;: Used to keep track of multiple function calls. Think of it as a stack of dishes that pile up, and when it comes time to wash them, the best way to do it is to take the dishes from the top until you get to the last one. If it sees an API call it sends it to the Web API container&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web APIs&lt;/strong&gt;: those are the JavaScript programming APIs you can use to build apps on the Web. Some of the browser apis we have available are the DOM, the Fetch API, the Canvas API, and much more. So any event, API request, setTimeOut, will be placed on here until the event got fired (click a button, scroll) or we received the data from a request (fetching data from a server).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The callback queue&lt;/strong&gt;: here will be receiving the callbacks from the Web API container. It works in a way that the first callback received is the first one sent to the call stack to be executed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event loop&lt;/strong&gt;: a mechanism that manages the order of execution. It constantly checks the call stack and the callback queue. So when the call stack is empty the event loop immediately passes the callbacks in the queue for its execution. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is where the magic happens. The solution to this problem was resolved inside the javascript runtime environment, additional threads are being started to handle different tasks. &lt;/p&gt;

&lt;h2&gt;
  
  
  3.Asynchronous functions exposed by the Web API extend the language
&lt;/h2&gt;

&lt;p&gt;Now we can see that even though there is the main thread in which all the code we write in our programs is executed (call stack)  there are asynchronous functions exposed by the Web API in the browser that perform some operations (fetch data, set local storage, dom manipulation) that do not block the main thread simply because they are not performed in that thread ( waiting on the callback queue). &lt;/p&gt;

&lt;p&gt;Important to realize that the concept of more than one thing happening at the same time, or multiple related things happening without waiting for the previous one to be completed is the base of the term Asynchronous.&lt;/p&gt;

&lt;p&gt;Therefore, with all of the above, we can conclude that &lt;em&gt;Javascript is asynchronous and not blocking&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Next, we will start to talk about the different mechanisms javascript has brought to the table in order to control and manage these asynchronous actions. Those mechanisms are, as you can guest: callbacks, promises, and async/await functions. &lt;/p&gt;

&lt;p&gt;Thanks for reading and if you have any suggestions please don't hesitate to leave a  comment, I will be happy to take into account your recommendations.&lt;/p&gt;

&lt;p&gt;I'll also leave bellow some links where you can find more info:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/8aGhZQkoFbQ"&gt;https://youtu.be/8aGhZQkoFbQ&lt;/a&gt; &lt;br&gt;
&lt;a href="https://eloquentjavascript.net/11_async.html"&gt;https://eloquentjavascript.net/11_async.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://medium.com/@gemma.stiles/understanding-the-javascript-runtime-environment-4dd8f52f6fca"&gt;https://medium.com/@gemma.stiles/understanding-the-javascript-runtime-environment-4dd8f52f6fca&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>React Challenge: Autocomplete functionality in React from scratch</title>
      <dc:creator>CinArb/</dc:creator>
      <pubDate>Wed, 16 Mar 2022 00:54:55 +0000</pubDate>
      <link>https://forem.com/cinarb2/react-challenge-autocomplete-functionality-in-react-from-scratch-3g0m</link>
      <guid>https://forem.com/cinarb2/react-challenge-autocomplete-functionality-in-react-from-scratch-3g0m</guid>
      <description>&lt;p&gt;In today's challenge, we will look at implementing autocomplete functionality in React and how to improve the performance of this approach by using the debounce function and the useMemo hook.&lt;/p&gt;

&lt;p&gt;I’ll be using a function to call &lt;a href="https://rickandmortyapi.com/"&gt;the Rick and Morty REST API&lt;/a&gt; to return all the locations from the show.   &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the Search Bar
&lt;/h3&gt;

&lt;p&gt;I’ll just have a single component called App that includes a form tag. Inside the form, we have the input and datalist element. &lt;/p&gt;

&lt;p&gt;With the input element, we will be reading the location the user is typing and then we can bind the datalist to the input. This will provide an autocomplete feature and the user can see a drop-down list with suggestions.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&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="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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="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="s1"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// state that controlled the input value&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;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setQuery&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="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// state that hold API data&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;suggestion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSuggestion&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getLocations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;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="s2"&gt;`https://rickandmortyapi.com/api/location/?name=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;//only add the data with the list of locations to the suggestion array&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setSuggestion&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;))&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;err&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;//handle error when user types location that doesn’t exist from API&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;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;setSuggestion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;clear&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="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
      &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Type location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;query&lt;/span&gt;&lt;span class="dl"&gt;'&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;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&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;setQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;getLocations&lt;/span&gt;&lt;span class="p"&gt;()}}&lt;/span&gt;
      &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;locations&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;datalist&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;locations&lt;/span&gt;&lt;span class="dl"&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;query&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;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="c1"&gt;// required to avoid the dropdown list to display the locations fetched before&lt;/span&gt;
      &lt;span class="nx"&gt;suggestion&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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;//make sure to only display locations that matches query&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;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&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;option&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&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;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/datalist&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one state variable called suggestion. That’s going to hold the info we receive from the API &lt;/li&gt;
&lt;li&gt;getLocations() that enclose the axios request and will be called when the user is typing on the search bar. &lt;/li&gt;
&lt;li&gt;The URL we pass through axios will contain the query we get from input&lt;/li&gt;
&lt;li&gt;From the response, we only want the results array, that contains the name of the locations.&lt;/li&gt;
&lt;li&gt;We need to catch errors when the user types a location that does not exist. The browser by default will be throwing errors to the console if we continue typing a location that does not exist. So we added console.clear() to avoid that. &lt;/li&gt;
&lt;li&gt;Finally, as we receive the info, we will be mapping through the array and set the value of the option equal to the name of the location. It’s important to add the key property so we don’t get an error. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/autocomplete-zmw5ln?file=/src/App.js"&gt;https://codesandbox.io/s/autocomplete-zmw5ln?file=/src/App.js&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You can take a look at the above codesanbox and see that it works. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem:
&lt;/h3&gt;

&lt;p&gt;Although we have accomplished the task, we must bear in mind that it’s very inefficient to make one API call per keystroke. Imagine in a real project scenario, we could harm the performance of the application and also saturate the API. &lt;/p&gt;

&lt;h3&gt;
  
  
  The solution:
&lt;/h3&gt;

&lt;p&gt;One of the ways to avoid this is to use a function called debounce which helps us to postpone the execution of the function by a few milliseconds and therefore cancel the previous calls and execute the new one. &lt;/p&gt;

&lt;p&gt;If you want to know in-depth about debounce functions feel free to click on &lt;a href="https://www.joshwcomeau.com/snippets/javascript/debounce/"&gt;here&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wait&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;timerId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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;timerId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="nx"&gt;timerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;timerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
       &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;wait&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;p&gt;In our case, we are going to pass as a callback the function getLocations with a delay of 300 milliseconds.&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;input&lt;/span&gt;
      &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Type location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;query&lt;/span&gt;&lt;span class="dl"&gt;'&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;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{(&lt;/span&gt;&lt;span class="nx"&gt;e&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;setQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getLocations&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;))}}&lt;/span&gt;
      &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;locations&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we try to implement the debounce function in React we will see that nothing happens. The reason why is that each time the user types we are making a new rendering and therefore generating different instances of the debounce function. &lt;/p&gt;

&lt;p&gt;As we don't want to generate different instances but to preserve the same one we must seek the help of a hook called useMemo.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&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="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;useMemo&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&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;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setQuery&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="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="c1"&gt;// state that hold API data&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;suggestion&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSuggestion&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getLocations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;setQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&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="s2"&gt;`https://rickandmortyapi.com/api/location/?name=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;then&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setSuggestion&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;))&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;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;404&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;setSuggestion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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;clear&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="p"&gt;};&lt;/span&gt;

 &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wait&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;timerId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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;timerId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;clearTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="nx"&gt;timerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setTimeout&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;timerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
       &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;wait&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;debouncedResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getLocations&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

 &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
       &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
       &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Type location&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
       &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;query&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
       &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;debouncedResults&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;locations&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
     &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;datalist&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;locations&lt;/span&gt;&lt;span class="dl"&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;query&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;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="c1"&gt;// // required to avoid the dropdown list to display the locations fetched before&lt;/span&gt;
         &lt;span class="nx"&gt;suggestion&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&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;option&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;}&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;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&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="p"&gt;;&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;/datalist&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Search&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can see that we have implemented the hook useMemo. Basically what it does is to save the instance of the debounce function and not create new ones every time the user types in the search bar. &lt;/p&gt;

&lt;p&gt;That’s all we needed. You can see the final result in the following codesandbox link: &lt;a href="https://codesandbox.io/s/autocomplete-debounce-function-and-usememo-e1qzfy?file=/src/App.js:0-1588"&gt;https://codesandbox.io/s/autocomplete-debounce-function-and-usememo-e1qzfy?file=/src/App.js:0-1588&lt;/a&gt; &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Understanding Context API</title>
      <dc:creator>CinArb/</dc:creator>
      <pubDate>Fri, 11 Mar 2022 20:52:45 +0000</pubDate>
      <link>https://forem.com/cinarb2/understanding-context-api-ejd</link>
      <guid>https://forem.com/cinarb2/understanding-context-api-ejd</guid>
      <description>&lt;p&gt;React context API was introduced in 2018 in version 16.3.0. Its main purpose is to manage global states. It’s a really good alternative to prop drilling. In other words, sending props from higher level components to lower levels, that means passing data around to components that don't even really need it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use Context Api:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Separate the code from your component structure. Normally you would create a folder called context and create different files depending on the context. Examples: Theme context, user authentication context, language context.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we import “create context” and proceed to use the method createContext() as follows.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kwGX1BI_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2qjvytha831eexmhrkr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kwGX1BI_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2qjvytha831eexmhrkr.png" alt="Image description" width="880" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now we are going to create the Provider component. The idea is to wrap all the components that need the data inside the Provider.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g5ma9cJd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iztpg5wf55bkfuge5a0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g5ma9cJd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iztpg5wf55bkfuge5a0u.png" alt="Image description" width="880" height="744"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As you can see on the code snippet above, we create the ThemeProvider and pass the children as a prop. We return the ThemeContext.Provider who is in charge of pass the data necessary to the children inside this component. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R3oXb7BG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6fadm3fxmis6g5og0rr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R3oXb7BG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6fadm3fxmis6g5og0rr.png" alt="Image description" width="772" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Export context and provider. remember not to ad {} on any data that we are going to export as default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Import the ThemeProvider the most near possible to the components that are going to use the data. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And finally, in order to consume the context, we need to  use the useContext hook. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W2fbpGmW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhmsenarpll9fpqdgatx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W2fbpGmW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhmsenarpll9fpqdgatx.png" alt="Image description" width="880" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this final line we use destructuring to only bring the data we need on that particular  component. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions:
&lt;/h2&gt;

&lt;p&gt;Context API is a great option for small projects because it is a built in package and thus, does not extend the size of our project. As you can see It’s really easy to understand. &lt;/p&gt;

&lt;p&gt;However, context api is not advisable for large-scale applications. This is because every time the value of the context changes all the consumer components rerender. And this can become a nightmare when it comes to your app performance. &lt;/p&gt;

</description>
      <category>react</category>
      <category>contextapi</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
