<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nihar Raote</title>
    <description>The latest articles on DEV Community by Nihar Raote (@napoleon039).</description>
    <link>https://dev.to/napoleon039</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%2F60962%2F6f8d4a54-92c0-451e-951f-35ea231b7bba.jpg</url>
      <title>DEV Community: Nihar Raote</title>
      <link>https://dev.to/napoleon039</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/napoleon039"/>
    <language>en</language>
    <item>
      <title>How to create a simple PostgreSQL database with Expressjs?</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Mon, 10 Jul 2023 17:10:03 +0000</pubDate>
      <link>https://dev.to/napoleon039/how-to-create-a-simple-postgresql-database-with-expressjs-58n8</link>
      <guid>https://dev.to/napoleon039/how-to-create-a-simple-postgresql-database-with-expressjs-58n8</guid>
      <description>&lt;p&gt;PostgreSQL is a powerful database with features like multi-version concurrency control, a customizable storage interface for tables, a robust access-control system, support for full-text search among many others. It can be used with a variety of back-end languages like Nodejs, Rust, Python, Java, etc.&lt;/p&gt;

&lt;p&gt;It is not difficult to setup a PostgreSQL database with a framework like Expressjs. This is a simple guide on creating a set of CRUD APIs. As long as you know the basics of Node and Express, you can follow along. Some experience with SQL databases (especially PostgreSQL) will help you understand this guide much better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an Express server
&lt;/h2&gt;

&lt;p&gt;I will be creating a basic Express server:&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%2Fyl489qfvjkkduemavvw1.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%2Fyl489qfvjkkduemavvw1.png" alt="Creating a simple Express server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A PostgreSQL table
&lt;/h2&gt;

&lt;p&gt;We have our express server. Let's create a table in PostgreSQL. I will log in to the default database with my role and create a new database for our table. There will be a link at the end of this article about creating roles in PostgreSQL.&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%2Fgsdx9ryjqd8oe3oc2anq.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%2Fgsdx9ryjqd8oe3oc2anq.png" alt="Creating a PostgreSQL table."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, with &lt;code&gt;-d&lt;/code&gt; we specify a database. &lt;code&gt;postgres&lt;/code&gt; is the default one. And &lt;code&gt;-U&lt;/code&gt; specifies our role name. My role's name is &lt;code&gt;personal&lt;/code&gt;. I created a database named &lt;code&gt;userAPI&lt;/code&gt; and switched to it.&lt;/p&gt;

&lt;p&gt;We have an empty &lt;code&gt;users&lt;/code&gt; table in the &lt;code&gt;userAPI&lt;/code&gt; database. We need to access this database from Express.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting to the database
&lt;/h2&gt;

&lt;p&gt;To communicate with our PostgreSQL database and table, we will need a package. The package we will be using is called &lt;a href="https://www.npmjs.com/package/pg" rel="noopener noreferrer"&gt;&lt;em&gt;node-postgres&lt;/em&gt;&lt;/a&gt;.&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%2F5pqu78ryybm9vjhs5bc7.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%2F5pqu78ryybm9vjhs5bc7.png" alt="Installing the node-postgres package with npm and yarn."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's use this package to start a connection with our database.&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%2Fjcgtml7kpdvo0zt92w7x.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%2Fjcgtml7kpdvo0zt92w7x.png" alt="Connecting to the database table with the installed package."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have put the required configuration in a separate file. A new pool of connections is created with the following properties:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;user - This is the user account/role we created that can access the &lt;code&gt;userAPI&lt;/code&gt; database.&lt;/li&gt;
&lt;li&gt;host - Since we are hosting Postgres on our own machine, localhost would be used. If you are connecting to a remote PostgreSQL database, then use the remote machine's address. For eg - &lt;a href="http://86.152.120.12" rel="noopener noreferrer"&gt;http://86.152.120.12&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;database - This is the database we would like to access.&lt;/li&gt;
&lt;li&gt;password - The corresponding password for the user account/role.&lt;/li&gt;
&lt;li&gt;port - By default the port is &lt;em&gt;5432&lt;/em&gt;. If you have configured a different port or are connecting to your remote machine, use the appropriate port number.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have our Express server, a PostgreSQL database, an empty table, and the server can access the database. Let's create some APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple CRUD
&lt;/h2&gt;

&lt;p&gt;We will perform simple CRUD operations on our database - creating a new user, reading data about a user, updating a user's data, and deleting a user.&lt;/p&gt;

&lt;p&gt;We will keep them in a separate file as well.&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%2Fqrvba81doj916ksk45ab.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%2Fqrvba81doj916ksk45ab.png" alt="This is an API for creating new users."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After extracting the name and email sent in the request, we use the &lt;code&gt;query&lt;/code&gt; function to create a new record in the database. If you have used MySQL with Express before, this syntax will be very familiar.&lt;/p&gt;

&lt;p&gt;We can read the data for all users or a single one:&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%2F30k9946ewlxb947me7pf.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%2F30k9946ewlxb947me7pf.png" alt="These are 2 APIs for fetching all users and fetching a single user by their id."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These three functions also demonstrate how parameters are passed to SQL statements in &lt;em&gt;node-postgres&lt;/em&gt;. If you have five parameters, then each parameter will be picked from the array in order. For example:&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%2Fyjsp2datt1tlj6gsp031.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%2Fyjsp2datt1tlj6gsp031.png" alt="An example of passing parameters to an SQL query."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's create the update and delete functions:&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%2F90vg431v4w4wq8n5ta24.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%2F90vg431v4w4wq8n5ta24.png" alt="These are 2 APIs that update a user and delete a user by using the user's id."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure to export the queries so we can use them to create endpoints:&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%2F0w6r88274yavvb9guw9k.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%2F0w6r88274yavvb9guw9k.png" alt="API endpoints for all the queries."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you have never worked with PostgreSQL before, I hope you try it.&lt;/p&gt;

&lt;p&gt;Here are some additional resources:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;a href="https://www.postgresqltutorial.com/postgresql-administration/postgresql-roles/" rel="noopener noreferrer"&gt;tutorial&lt;/a&gt; on creating roles in PostgreSQL.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.prisma.io/dataguide/postgresql/benefits-of-postgresql" rel="noopener noreferrer"&gt;Why use PostgreSQL&lt;/a&gt;?&lt;/li&gt;
&lt;li&gt;How does PostgreSQL stack up against another equally popular database like MongoDB? Read &lt;a href="https://www.mongodb.com/compare/mongodb-postgresql" rel="noopener noreferrer"&gt;this article&lt;/a&gt; about their differences and how to pick between them.&lt;/li&gt;
&lt;li&gt;A fan of Sequelize? Learn &lt;a href="https://www.robinwieruch.de/postgres-express-setup-tutorial/" rel="noopener noreferrer"&gt;how to use&lt;/a&gt; it with PostgreSQL and Express.&lt;/li&gt;
&lt;li&gt;After creating a CRUD API with Express and PostgreSQL, you can build a front-end with Vue and have a &lt;a href="https://www.bezkoder.com/vue-node-express-postgresql/" rel="noopener noreferrer"&gt;full-stack&lt;/a&gt; application.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>postgres</category>
      <category>express</category>
      <category>node</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Custom React Hooks. What are they and how are they helpful?</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Fri, 26 Aug 2022 14:57:00 +0000</pubDate>
      <link>https://dev.to/napoleon039/custom-react-hooks-what-are-they-and-how-are-they-helpful-p6h</link>
      <guid>https://dev.to/napoleon039/custom-react-hooks-what-are-they-and-how-are-they-helpful-p6h</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;React Hooks were introduced in React.js v16.8. They allow us to use state in functional components, similar to the lifecycle hooks of class-based components. For every situation, there is a React Hook you can use. From the frequently used hooks like &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; to hooks like &lt;code&gt;useCallback&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt;, you can take your time to browse &lt;a href="https://reactjs.org/docs/hooks-reference.html"&gt;the documentation&lt;/a&gt; for the hook you need.&lt;/p&gt;

&lt;p&gt;Since there are hooks for almost every situation you might encounter, is there a need for your own React Hook?&lt;/p&gt;

&lt;h2&gt;
  
  
  What are custom hooks?
&lt;/h2&gt;

&lt;p&gt;When we want to re-use some logic in multiple places, we extract it to a function. If we also want to re-use some JSX along with it, we extract it to a separate component. Similarly, a React Hook is also a function.&lt;/p&gt;

&lt;p&gt;If we want to create a reusable function that also uses React Hooks like &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt;, we cannot store this logic in a plain function.&lt;/p&gt;

&lt;p&gt;A component can be created to store this reusable logic so we can then use hooks. But this is not ideal if we do not have any JSX to write in that component.&lt;/p&gt;

&lt;p&gt;In such situations, the reusable logic can be extracted to a custom React Hook. Making a custom React Hook is simple - prefix a function's name with &lt;em&gt;use&lt;/em&gt;. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BbKfEgxU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cuq5qnqrx41who5rha8l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BbKfEgxU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cuq5qnqrx41who5rha8l.png" alt="Regular function vs a custom hook" width="880" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  An example of a custom hook
&lt;/h2&gt;

&lt;p&gt;Here is some logic that has been extracted into a custom hook.&lt;/p&gt;

&lt;p&gt;The custom hook &lt;code&gt;useSelectClub&lt;/code&gt; takes an array of sports clubs as an argument. A person can belong to one or more sports clubs. If a club is marked as a person's favorite, then that club's code is returned. If the user belongs to a single club, that club's code is returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VFLD4dEk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlnl14u8azr6oba08coh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VFLD4dEk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlnl14u8azr6oba08coh.png" alt="Example of a custom hook" width="880" height="679"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;I hope this short guide helped you to understand custom hooks in React and how to use them.&lt;/p&gt;

&lt;p&gt;Just like how recurring JSX and logic can be turned into reusable components, logic that uses React Hooks can also be turned into reusable custom hooks.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Improve DOM Structure with React Portal</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Mon, 30 May 2022 13:15:36 +0000</pubDate>
      <link>https://dev.to/napoleon039/how-to-improve-dom-structure-with-react-portal-5fbd</link>
      <guid>https://dev.to/napoleon039/how-to-improve-dom-structure-with-react-portal-5fbd</guid>
      <description>&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;According to the &lt;a href="https://reactjs.org/docs/portals.html"&gt;official React documentation&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Portals provide a first-class way to render children into a DOM node that exists outside a DOM hierarchy of the parent component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Take a look at this example - let's say you have a &lt;code&gt;Calculator&lt;/code&gt; component, inside which there is a &lt;code&gt;Buttons&lt;/code&gt; component. It would look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--34yFFRv4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxyzo4kefqo9nybtv8ra.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--34yFFRv4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxyzo4kefqo9nybtv8ra.png" alt="An example of nested components" width="880" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The root component &lt;code&gt;App&lt;/code&gt; along with all its child components will be rendered inside a single DOM node (most likely a div with an id of &lt;code&gt;#root&lt;/code&gt;). With React Portal, a child component like &lt;code&gt;Buttons&lt;/code&gt; can be rendered outside the div with an id of &lt;code&gt;#root&lt;/code&gt;. You can imagine the structure of the DOM to look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BG6jaTqz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2xsr9phdsz4evzwzq40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BG6jaTqz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2xsr9phdsz4evzwzq40.png" alt="DOM structure of nested components" width="880" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should I know this?
&lt;/h2&gt;

&lt;p&gt;In certain situations, it is necessary to render some JSX or a component outside the root element. The best example of such a situation would be a modal.&lt;/p&gt;

&lt;p&gt;A modal would typically cover the entire screen. One would expect this to be reflected in the DOM structure as well. However, that's not what happens in React. If a modal is nested inside a component, that is also how the DOM structure would look like. This can be confusing for people who use screen readers. From an accessibility standpoint, this is an issue.&lt;/p&gt;

&lt;p&gt;Resolving this might be a bit easy to do in Vanilla JS and HTML, but harder to do in React. Since React applications have a single mount point, putting an element or component outside this mount point is a challenge. With React Portal, this becomes an easy task.&lt;/p&gt;

&lt;p&gt;Although the element or component will be &lt;em&gt;portaled&lt;/em&gt; to a different DOM node, it will still function as a regular React element. For our modal this means that it is in a different DOM node than the mount point. But it can still receive props and state and behave like a regular nested React element.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I use it?
&lt;/h2&gt;

&lt;p&gt;Let's use the modal example from the section above. Creating a React Portal and mounting it to a different DOM node can be done in 2 steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1:
&lt;/h3&gt;

&lt;p&gt;Create another DOM node for mounting the modal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xFQCvCTA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dt54wepkzejdl9021xu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xFQCvCTA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dt54wepkzejdl9021xu.png" alt="Creating a separate DOM node" width="880" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The React application will be mounted to the div with an id of &lt;code&gt;#root&lt;/code&gt;. And the modal will be mounted to the div with an id of &lt;code&gt;#modal-root&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2:
&lt;/h3&gt;

&lt;p&gt;Create a Portal wrapper component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YbVcLaPj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/353nz3778teghcb357ld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YbVcLaPj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/353nz3778teghcb357ld.png" alt="Creating a wrapper component with React Portals" width="880" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This wrapper component will take a child element and create a Portal for it. The &lt;code&gt;useEffect&lt;/code&gt; hook mounts the element and cleans up when the component is unomunted.&lt;/p&gt;

&lt;p&gt;Here is how it might look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WA33c7Th--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jfkduvspr5687h81md4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WA33c7Th--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jfkduvspr5687h81md4.png" alt="Wrapping the Modal in the wrapper component" width="880" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though the &lt;code&gt;Modal&lt;/code&gt; component is nested in the app hierarchy, it will look different in the DOM structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--26atKFiE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qneiqnrxh6wze23usdko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--26atKFiE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qneiqnrxh6wze23usdko.png" alt="New DOM structure after using React Portals" width="880" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the real DOM structure, you won't see the &lt;code&gt;Modal&lt;/code&gt; component directly like this, but a div instead. This is simply a demonstration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extra resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://blog.logrocket.com/learn-react-portals-by-example/"&gt;This article&lt;/a&gt; has another good use-case for using React Portals.&lt;/li&gt;
&lt;li&gt;A &lt;a href="https://malcoded.com/posts/react-portals/"&gt;dialog component&lt;/a&gt; is also a good scenario where React Portals is quite handy.&lt;/li&gt;
&lt;li&gt;Do you need a ref from a React Portal? Then &lt;a href="https://stackoverflow.com/a/46644072/12903901"&gt;this solution&lt;/a&gt; for React 16 might help.&lt;/li&gt;
&lt;li&gt;Learn &lt;a href="https://dev.to/alexandprivate/your-next-react-modal-with-your-own-usemodal-hook-context-api-3jg7"&gt;how to create&lt;/a&gt; a Modal component with React Hooks, Context, and Portals.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>These 3 differences between Reactjs and React Native are easy to spot.</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Tue, 12 Apr 2022 17:56:14 +0000</pubDate>
      <link>https://dev.to/napoleon039/these-3-differences-between-reactjs-and-react-native-are-easy-to-spot-53d0</link>
      <guid>https://dev.to/napoleon039/these-3-differences-between-reactjs-and-react-native-are-easy-to-spot-53d0</guid>
      <description>&lt;h2&gt;
  
  
  What is React Native?
&lt;/h2&gt;

&lt;p&gt;It is a JavaScript framework that is used to build cross-platform applications. React Native uses React.js. Because of this, opening a file from a React Native project, a React developer will find the structure and syntax used quite familiar. A React Native project can be packaged for both Android and iOS.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are the differences between React and React Native code?
&lt;/h2&gt;

&lt;p&gt;There are many things that are different between React and React Native. These differences become more prominent the closer we are to production.&lt;/p&gt;

&lt;p&gt;During development, the differences might not seem much, but when pushing the code to a production environment, there is a noticeable difference between the two. After all, for Reactjs, the final result would most probably be a website. For React Native, it would be a mobile app.&lt;/p&gt;

&lt;p&gt;Here are 3 primary differences a Reactjs developer will encounter when working with React Native for the first time.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Grouping elements
&lt;/h3&gt;

&lt;p&gt;In React, to group some elements together and to create visual blocks of code, we mainly use &lt;code&gt;div&lt;/code&gt;. Here's an example:&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%2F7cwk05cc6tfsz6b31oqu.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%2F7cwk05cc6tfsz6b31oqu.png" alt="Grouping elements in Reactjs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, this code will not work in React Native. Since React Native code needs to map to native components on mobile, we need to use the &lt;code&gt;View&lt;/code&gt; component instead of &lt;code&gt;div&lt;/code&gt;. Let's change the above example to work in React Native:&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%2Fy191wv6j1r1cfid7b34n.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%2Fy191wv6j1r1cfid7b34n.png" alt="Grouping elements in React Native"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though React Native uses Reactjs, in the end, the code needs to run on mobile devices. UI components like &lt;code&gt;View&lt;/code&gt; help to map React Native code to native UI code.&lt;/p&gt;

&lt;p&gt;So, in a React Native application, &lt;code&gt;div&lt;/code&gt;s are no longer used. The &lt;code&gt;View&lt;/code&gt; component needs to be used instead. You can think of the &lt;code&gt;View&lt;/code&gt; component as React Native's alternative for &lt;code&gt;div&lt;/code&gt;. When it comes to differences between React and React Native, these UI components stand out easily. &lt;/p&gt;

&lt;p&gt;Speaking of differences, did you notice how the &lt;code&gt;row&lt;/code&gt; and &lt;code&gt;container&lt;/code&gt; classes were applied in React Native? That is the second difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Styling without CSS files
&lt;/h3&gt;

&lt;p&gt;Unlike React, React Native does not style components with CSS files. Any styles needed are supposed to be created with the &lt;code&gt;StyleSheet&lt;/code&gt; component. Let's use the example from the first point. This time, take a look at the styles as well:&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%2Fmeyh3cf8hm72uw68vk8f.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%2Fmeyh3cf8hm72uw68vk8f.png" alt="Styling in React Native"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Does this look familiar? It should. This is similar to how inline styles are written in React. There are also a few differences.&lt;/p&gt;

&lt;p&gt;In React, &lt;code&gt;padding: '20px'&lt;/code&gt; would be a typical line in an inline style. In React Native, this would instead be &lt;code&gt;padding: 20&lt;/code&gt;. Of course, something like &lt;code&gt;padding: '5%'&lt;/code&gt; is written the same way in React Native.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Do keep in mind that some style attributes might not work with relative units like &lt;code&gt;% em rem&lt;/code&gt;.&lt;/em&gt; If you want to use these relative units for these attributes, there is a package available. It will be linked at the end of this article.&lt;/p&gt;

&lt;p&gt;Another thing to keep in mind is that the shorthand syntax for various CSS attributes is not available in React Native. For example, take a look at the following comparison between a property inside a CSS file and its equivalent in React Native:&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%2F647fupl67avlt3aqgbzl.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%2F647fupl67avlt3aqgbzl.png" alt="CSS shorthands"&gt;&lt;/a&gt;&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%2Fodx1rkliiokiov0x3v67.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%2Fodx1rkliiokiov0x3v67.png" alt="React Native longhands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although these longhand forms can also be used in CSS, it's the shorthand form that is usually used for convenience. In React Native, however, these shorthands cannot be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Default layout for all elements
&lt;/h3&gt;

&lt;p&gt;In React Native, all components have a display of flex and direction of column applied by default. So there is no need to specify &lt;code&gt;display: flex&lt;/code&gt;. Since flexbox is enabled everywhere by default, it becomes easier to style the layout.&lt;/p&gt;

&lt;p&gt;I found it a little confusing in the beginning since I was used to the default flex direction of row. Over time I &lt;em&gt;did&lt;/em&gt; get used to the column direction, but do keep that flex direction in mind before you start styling a bunch of nested components. &lt;/p&gt;

&lt;p&gt;I think this default direction makes sense if you consider how a mobile device is usually held in portrait mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;These are some of the few differences that will seem unfamiliar to you when working with React Native for the first time. Pushing a React Native app to production can seem confusing to a developer who has never experienced that before.&lt;/p&gt;

&lt;p&gt;This is also what makes React Native easier to get started with if you have worked with Reactjs.&lt;/p&gt;

&lt;p&gt;The differences between React Native and React are because of different targets - a mobile application instead of a web application. Other than that, the rest of the development experience is similar to working with Reactjs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Helpful resources
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/react-native-responsive-screen" rel="noopener noreferrer"&gt;This package&lt;/a&gt; is a workaround for using the percentage unit for a responsive UI.&lt;/li&gt;
&lt;li&gt;The official documentation on deploying React Native applications for both &lt;a href="https://reactnative.dev/docs/signed-apk-android" rel="noopener noreferrer"&gt;Android&lt;/a&gt; and &lt;a href="https://reactnative.dev/docs/publishing-to-app-store" rel="noopener noreferrer"&gt;iOS&lt;/a&gt; is well written.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://instabug.com/blog/react-native-app-ios-android/" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is another great article on deploying React Native applications.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.expo.dev" rel="noopener noreferrer"&gt;Expo&lt;/a&gt; is a framework that helps quickly develop, build, and deploy React Native applications.&lt;/li&gt;
&lt;li&gt;Does your React Native application have a complex navigation structure? &lt;a href="https://medium.com/@jan.hesters/building-a-react-native-app-with-complex-navigation-using-react-navigation-85a479308f52" rel="noopener noreferrer"&gt;This article&lt;/a&gt; might help you out.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>reactnative</category>
    </item>
    <item>
      <title>React Context and Hooks: Pass data and update it easily</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Thu, 24 Feb 2022 18:33:56 +0000</pubDate>
      <link>https://dev.to/napoleon039/react-context-and-hooks-pass-data-and-update-it-easily-3akb</link>
      <guid>https://dev.to/napoleon039/react-context-and-hooks-pass-data-and-update-it-easily-3akb</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/napoleon039/react-context-and-hooks-an-excellent-way-to-pass-data-252p"&gt;previous article&lt;/a&gt;, we looked at what Context is and how to pass values with it. In this article we will look at updating the Context value from the child component. For our theme example this means clicking the button will now switch the theme between light and dark.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating Context value
&lt;/h2&gt;

&lt;p&gt;In the previous article, we directly passed the theme via the Provider component. Although the value is passed to &lt;code&gt;ThemeButton.js&lt;/code&gt; and &lt;code&gt;ThemeWindow.js&lt;/code&gt; components, we cannot update this value.&lt;/p&gt;

&lt;p&gt;To switch between the two theme types, we need to pass a function as well. This function will allow us to switch between both themes.&lt;/p&gt;

&lt;p&gt;So we need to pass this function along with the dark theme via the Provider component. To do this, we can create a stateful object which will contain both the theme and a function. This way React can keep track of the theme and perform the necessary re-renders when it updates.&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%2F1delo9s2m52ccyc97o69.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%2F1delo9s2m52ccyc97o69.png" alt="Creating a stateful object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is pretty straightforward. A stateful object containing a theme type and a function is passed via the Provider component. While the &lt;code&gt;ThemeWindow&lt;/code&gt; component only needs minor changes, we do need to implement the function in the &lt;code&gt;ThemeButton&lt;/code&gt; component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing theme switch function
&lt;/h2&gt;

&lt;p&gt;Nothing special needs to be done to implement the function. Calling it when the button is pressed will switch the theme.&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%2F3bavb7fx2pdvzum1833s.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%2F3bavb7fx2pdvzum1833s.png" alt="Implementing theme switch function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As easy as that. When this button is clicked, the &lt;code&gt;switchTheme()&lt;/code&gt; function will be executed. This will update the stateful value and thus trigger a re-render. And finally, the updated theme will be rendered in the &lt;code&gt;ThemeWindow&lt;/code&gt; component.&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%2Fkabnjypr0b3kdw4eo6ab.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%2Fkabnjypr0b3kdw4eo6ab.png" alt="Minor changes in ThemeWindow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;This was the conclusion to the previous article about React Context. As you have seen, React Context is a powerful tool. &lt;/p&gt;

&lt;p&gt;However, this simple example does not really showcase its power. In complex applications with dozens of components, where values and state need to be passed to several different components, React Context will make this job far easier than regular props.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>React Context and Hooks: An excellent way to pass data</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Wed, 26 Jan 2022 09:59:28 +0000</pubDate>
      <link>https://dev.to/napoleon039/react-context-and-hooks-an-excellent-way-to-pass-data-252p</link>
      <guid>https://dev.to/napoleon039/react-context-and-hooks-an-excellent-way-to-pass-data-252p</guid>
      <description>&lt;h2&gt;
  
  
  What is Context?
&lt;/h2&gt;

&lt;p&gt;A Context is basically a JavaScript object that can be passed from one parent component to several child components efficiently. Props can pass values to components as well. But, if a value needs to be passed to a child component deep in the component tree, using props means the value also passes through components that do not need it. Or, if a value is required by several components, props can make it difficult.&lt;/p&gt;

&lt;p&gt;This is a good use case for Context. Props need to be passed from one component to the other. With Context, the parent component provides the value, and the child components that need it can access it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Passing values with Context and Hooks
&lt;/h2&gt;

&lt;p&gt;Let's take a common example used for demonstrating Context - themes. Consider the following UI:&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%2F4zltfqix0mihrgwleiun.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%2F4zltfqix0mihrgwleiun.png" alt="Image of app UI with various components highlighted"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the one with the red border is the main &lt;code&gt;App.js&lt;/code&gt; component. The one with the green border is a component called &lt;code&gt;ThemeButton.js&lt;/code&gt;, and the one with a blue border is the &lt;code&gt;ThemeWindow.js&lt;/code&gt; component. The &lt;code&gt;App&lt;/code&gt; component will have the other two as its child components. Clicking the button in &lt;code&gt;ThemeButton&lt;/code&gt; will toggle the theme between light and dark. The result of the theme will be reflected in &lt;code&gt;ThemeWindow&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We need to create a context first, then have the &lt;code&gt;App&lt;/code&gt; component &lt;em&gt;provide&lt;/em&gt; a value for its child components. The child component(s) will &lt;em&gt;consume&lt;/em&gt; this provided value. Let's set this up.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Creating a context
&lt;/h3&gt;

&lt;p&gt;A context can be created in a separate file or in the &lt;code&gt;App&lt;/code&gt; component itself, but it is generally better to create a separate file for it. Since depending on your app, you may need multiple contexts for different features. It can thus be useful to have separate files.&lt;/p&gt;

&lt;p&gt;Create a new file to hold the context and export it:&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%2Fejlksir2kz2afok635n7.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%2Fejlksir2kz2afok635n7.png" alt="Creating context theme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this file, we export two things - an object containing theme values and our context. The &lt;code&gt;createContext()&lt;/code&gt; function returns a Context object. It accepts an argument for the initial value similar to the &lt;code&gt;useState&lt;/code&gt; hook. In this case, the light themes object is the default value of &lt;code&gt;ThemeContext&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Passing values with the Provider component
&lt;/h3&gt;

&lt;p&gt;Since the context has been created, let's add it in &lt;code&gt;App.js&lt;/code&gt;.&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%2Fjb2khw6sq1ey7dubvsu2.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%2Fjb2khw6sq1ey7dubvsu2.png" alt="Passing values with Provider component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every Context object (created with &lt;code&gt;React.createContext()&lt;/code&gt;) has a &lt;strong&gt;Provider&lt;/strong&gt; component. This Provider component should be wrapped around all the child components that will be getting access to &lt;code&gt;ThemeContext&lt;/code&gt;. Conversely, there is also a &lt;strong&gt;Consumer&lt;/strong&gt; component.  You can also use the &lt;code&gt;useContext&lt;/code&gt; hook instead if you're not working with class-based components.&lt;/p&gt;

&lt;p&gt;You must be wondering why the &lt;strong&gt;light&lt;/strong&gt; object was used as the default value, but the Provider component has passed the &lt;strong&gt;dark&lt;/strong&gt; object. First of all, the &lt;code&gt;value&lt;/code&gt; prop used in the Provider component is something React recognizes so you cannot use a different name for that prop.&lt;/p&gt;

&lt;p&gt;Whatever is passed through the &lt;code&gt;value&lt;/code&gt; prop is what the child components consume. And if a component attempts to access &lt;code&gt;ThemeContext&lt;/code&gt; but does not have a &lt;strong&gt;Provider&lt;/strong&gt; component in the tree above, it will use the default value the Context object has (the light theme).&lt;/p&gt;

&lt;p&gt;As a quick example, let's say there was a third component called &lt;code&gt;ThemeFont.js&lt;/code&gt;. But this third component was not included within the Provider component - &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%2Fc5telth3u4sl37o6x7co.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%2Fc5telth3u4sl37o6x7co.png" alt="An example showing the scope of Provider"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;ThemeFont&lt;/code&gt; is not wrapped by the Provider component, it will not receive the new value. Although it can still access &lt;code&gt;ThemeContext&lt;/code&gt;, it will receive the default value, that is, the value we passed to &lt;code&gt;createContext&lt;/code&gt; - the light themes object.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Not wrapping a component that needs access to a Context in a Provider component still gives it access to the Context's default value. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Although you probably wouldn't have a reason to do this, it is still good to know.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Consuming context in child components
&lt;/h3&gt;

&lt;p&gt;Let's use React Hooks to consume the provided context value.&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%2Fd8m1hlqfnk7lumtnnpw3.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%2Fd8m1hlqfnk7lumtnnpw3.png" alt="Consuming context value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running this app, you would get the following result:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/vh0nd"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ThemeWindow&lt;/code&gt; certainly has the dark theme. But, the button to toggle the theme doesn't work. The next article will demonstrate how to update values passed via Context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some links to resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://kentcdodds.com/blog/how-to-use-react-context-effectively" rel="noopener noreferrer"&gt;How to use React Context effectively&lt;/a&gt; by &lt;a href="https://twitter.com/kentcdodds" rel="noopener noreferrer"&gt;Kent C. Dodds&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/context.html" rel="noopener noreferrer"&gt;React Context official docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Code splitting: Does this improve performance of React apps?</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Wed, 29 Sep 2021 07:15:39 +0000</pubDate>
      <link>https://dev.to/napoleon039/code-splitting-does-this-improve-performance-of-react-apps-1ma</link>
      <guid>https://dev.to/napoleon039/code-splitting-does-this-improve-performance-of-react-apps-1ma</guid>
      <description>&lt;p&gt;There are several ways to improve the performance of React applications. One of them is to make it &lt;em&gt;look&lt;/em&gt; faster to users. This is different from compressing asset files or making animations and transitions smoother.&lt;/p&gt;

&lt;p&gt;This article is about one such technique to improve the &lt;strong&gt;perceived performance&lt;/strong&gt; of a React application. Although there are other ways to improve performance, this article will focus on implementing code splitting in React with the Parcel bundler.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does code splitting mean?
&lt;/h2&gt;

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

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

&lt;p&gt;This means when a page initially loads, a smaller bundle will be retrieved from the server. Any additional code will be lazy loaded as needed. While the total amount of code is the same (and perhaps even a few bytes larger), the amount of code needed during initial load can be reduced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where should I use it? And why?
&lt;/h2&gt;

&lt;p&gt;By separating code into smaller bundles, load time is reduced, thus increasing performance. Even if you didn't change the code logic you wrote, the application certainly will seem faster. There is a link to an MDN page on perceived performance at the end of this article. &lt;/p&gt;

&lt;p&gt;That said, it does not mean you should split every component into bundles. Fetching a lot of small bundles is not that different from fetching a single large bundle. Whether it's reducing the number of requests or reducing time for initial load, both have their pros and cons.&lt;/p&gt;

&lt;p&gt;Your application might have components or files. The ones that might cause a page to load slowly are candidates for code splitting. For example, if there is a page with multiple images, split it. The page with images can load in the background while the other pages load before it and become interactive.&lt;/p&gt;

&lt;p&gt;React has some good features to help implement code splitting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code splitting React components
&lt;/h2&gt;

&lt;p&gt;There is no need for a configuration file to use code splitting with Parcel. Using the dynamic import syntax, &lt;code&gt;React.lazy&lt;/code&gt;, and &lt;code&gt;Suspense&lt;/code&gt;, we can code split a React component.&lt;/p&gt;

&lt;p&gt;First, you need a React project. I wrote an &lt;a href="https://niharraoteblog.netlify.app/how-to-create-a-minimal-react-and-parcel-app-in-5-steps"&gt;article&lt;/a&gt; on creating a minimal React project with Parcel.&lt;/p&gt;

&lt;p&gt;There is another package you need to install in addition to the dependencies of that project. It is a Babel plugin for parsing any dynamic imports. Install this in your project as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r6q_hSR8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eecmz3xla5le97ctiv8l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r6q_hSR8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eecmz3xla5le97ctiv8l.png" alt="Installing additional Babel plugin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And add it to the Babel config file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ffTVNb0v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t71zt34hvvsb4wxtu2y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ffTVNb0v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t71zt34hvvsb4wxtu2y.png" alt="Updated Babel configuration file after installing plugin"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create another component &lt;code&gt;Message.js&lt;/code&gt; in the &lt;code&gt;/src&lt;/code&gt; folder. A simple component will be enough to showcase how code splitting works in React.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EC57ttGT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8a1xsd4ko1pe43gotdmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EC57ttGT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8a1xsd4ko1pe43gotdmt.png" alt="Message component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now import it with the dynamic import syntax along with &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sr91Z8Rs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zk63b1d1jarn4poktod.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sr91Z8Rs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zk63b1d1jarn4poktod.png" alt="Importing packages along with Message component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The official React documentation offers an excellent explanation on the usage of &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt; for code splitting. A link to that page is also available at the end of this article.&lt;/p&gt;

&lt;p&gt;Everything is ready. Now let's run the project and compare the results&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HQYFJzaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q203dsebno05jc8jr8ow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HQYFJzaH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q203dsebno05jc8jr8ow.png" alt="Running project scripts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I know it works?
&lt;/h2&gt;

&lt;p&gt;After you run the project, it should open at &lt;code&gt;http://localhost:1234&lt;/code&gt;. Open the developer console and switch to the network tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Beeo4wE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mi2bbdniosioxww9psyz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Beeo4wE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mi2bbdniosioxww9psyz.png" alt="Screenshot of network tab showing running app with lazy loading"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yours might look a bit different, but you should see a separate bundle for the &lt;code&gt;Message&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;It will be interesting to see how it might look without code splitting the &lt;code&gt;Message&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;If you comment out the code splitting parts, import and use the &lt;code&gt;Message&lt;/code&gt; component normally:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--avlpia_---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rk0tt3ksr5sfj3g5e93h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--avlpia_---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rk0tt3ksr5sfj3g5e93h.png" alt="Commenting out lazy loading and Suspense code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And look at the network tab of the developer console again, you will not see that message bundle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jNOIamUh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy2kuu74jd7f1u6qgk83.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jNOIamUh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iy2kuu74jd7f1u6qgk83.jpg" alt="Screenshot of network tab showing running app after commenting out lazy loading"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;This is how you would implement code splitting in React with Parcel. Here are links to additional resources that might help.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Code_splitting"&gt;Definition of code splitting by MDN&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Performance/Perceived_performance"&gt;Read about Perceived Performance on MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://webpack.js.org/guides/code-splitting/"&gt;Webpack documentation on implementing code splitting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v2.parceljs.org/features/code-splitting/"&gt;Parcel documentation on code splitting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/code-splitting.html"&gt;Official React documentation about code splitting&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.smashingmagazine.com/2020/07/methods-performance-react-apps/"&gt;A more in-depth article by Smashing Magazine on improving performance in React apps&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>performance</category>
    </item>
    <item>
      <title>How many lines would it take to animate a button? GSAP makes it easy</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Thu, 16 Sep 2021 11:59:05 +0000</pubDate>
      <link>https://dev.to/napoleon039/how-many-lines-would-it-take-to-animate-a-button-gsap-makes-it-easy-1kgi</link>
      <guid>https://dev.to/napoleon039/how-many-lines-would-it-take-to-animate-a-button-gsap-makes-it-easy-1kgi</guid>
      <description>&lt;p&gt;In this article, I implement a simple button animation with GSAP and use the Composition API introduced in Vue 3.x. If you are not familiar with the Composition API, you can refer to the resources linked at the end of the article.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is GSAP?
&lt;/h2&gt;

&lt;p&gt;GSAP (GreenSock Animation Platform) is a JavaScript animation library that allows developers to create powerful animations with few lines of code. With this library, you get more precise control over your animations, which makes it easier to create complex animations. The GSAP library is small in size, easy to use, and works consistently across browsers with under-the-hood calculations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple button animation
&lt;/h2&gt;

&lt;p&gt;I have created a simple form and used GSAP to run an animation when the submit button is clicked. For this, I used the Composition API and ref from Vue. The animation itself takes only two lines of code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---MGMLOI1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akb3dtdnz9evaw24eqp6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---MGMLOI1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akb3dtdnz9evaw24eqp6.png" alt="Simple button animation code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the codepen; you can tweak around to make some cool form animations as well:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/NAPOLEON039/embed/NWgboLZ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://ihatetomatoes.net/simple-greensock-tutorial-your-first-steps-with-gsap/"&gt;Simple GreenSock Tutorial&lt;/a&gt; by Petr Tichy&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.freecodecamp.org/news/the-beginners-guide-to-the-greensock-animation-platform-7dc9fd9eb826/"&gt;A Beginner's Guide to GreenSock&lt;/a&gt; by Nicholas Kramer&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://css-tricks.com/how-to-animate-on-the-web-with-greensock/"&gt;How to Animate on the Web With GreenSock&lt;/a&gt; by Sarah Drasner&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v3.vuejs.org/api/composition-api.html"&gt;Official Vue docs on Composition API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.codemag.com/Article/2011041/Vue's-Composition-API"&gt;Vue Composition API&lt;/a&gt; by Shawn Wildermuth&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://auth0.com/blog/getting-started-with-vue-3-composition-api/"&gt;Comparing Composition API with Options API&lt;/a&gt; by Holly Guevara&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>vue</category>
      <category>gsap</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating a custom Express server in Nuxt.js: 2 things to remember</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Wed, 02 Jun 2021 13:28:07 +0000</pubDate>
      <link>https://dev.to/napoleon039/creating-a-custom-express-server-in-nuxt-js-2-things-to-remember-2g98</link>
      <guid>https://dev.to/napoleon039/creating-a-custom-express-server-in-nuxt-js-2-things-to-remember-2g98</guid>
      <description>&lt;h2&gt;
  
  
  Why would you want to create a custom server?
&lt;/h2&gt;

&lt;p&gt;A Server-side Rendered (SSR) Nuxt.js application requires a Node.js server. This server renders a webpage and passes it to the client-side Vuejs. This server is implemented by Nuxt itself behind the scenes. However, this simple server makes it difficult to implement things like a POST or GET request, a Pusher channel, cookies, etc.&lt;/p&gt;

&lt;p&gt;I needed to run a Puppeteer script on the server and thus extended the basic server provided by Nuxt.js by creating an Express server.&lt;/p&gt;

&lt;p&gt;For a server-side rendered application, a Node.js server needs to be configured. Although Nuxt provides one for you, you can extend this server with &lt;em&gt;serverMiddleware&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: While you can extend the server with &lt;em&gt;serverMiddleware&lt;/em&gt;, you can also extend and control the application's routing with &lt;em&gt;middleware&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Two things to keep in mind
&lt;/h2&gt;

&lt;p&gt;Here are 2 things to take note of when creating an Express server in your Nuxt.js application - &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Make sure to export the server
&lt;/h3&gt;

&lt;p&gt;In a simple JavaScript/Node.js application, an Express server will be the one controlling and managing most of the application. But, in a Nuxt.js application, you need to export it because you are &lt;em&gt;extending&lt;/em&gt; the server provided by Nuxt.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C3S4LvXY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weasho2a7pgd3fnev0df.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C3S4LvXY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weasho2a7pgd3fnev0df.png" alt="Exporting an Express server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Mention the server file path in Nuxt.js config
&lt;/h3&gt;

&lt;p&gt;This is mentioned in the &lt;a href="https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-servermiddleware#custom-api-endpoint"&gt;documentation&lt;/a&gt; as well. Here is what worked for me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CSroWmwh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d99wz0zmz5dux8fi3yfk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CSroWmwh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d99wz0zmz5dux8fi3yfk.png" alt="ServerMiddleware in Nuxtjs config"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It is very easy to extend the functionality of the default server provided by Nuxt.js. Remember, if you also want to control routing, you need to extend &lt;em&gt;middleware&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For creating my custom Express server and writing this article, I mainly used the &lt;a href="https://nuxtjs.org/docs/2.x/concepts/server-side-rendering"&gt;Nuxt.js documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>ssr</category>
      <category>vue</category>
    </item>
    <item>
      <title>Quick tips: Persistent wrapper content in Next.js</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Fri, 30 Apr 2021 17:44:49 +0000</pubDate>
      <link>https://dev.to/napoleon039/quick-tips-persistent-wrapper-content-in-next-js-31f1</link>
      <guid>https://dev.to/napoleon039/quick-tips-persistent-wrapper-content-in-next-js-31f1</guid>
      <description>&lt;h2&gt;
  
  
  What kind of content?
&lt;/h2&gt;

&lt;p&gt;Generally what comes to mind when thinking about some content that would stay constant across all or multiple pages, is a navigation bar. It can also be a header, some branding, or a menu.&lt;/p&gt;

&lt;p&gt;This is easy to do with the &lt;code&gt;_app.js&lt;/code&gt; component present in Next.js projects. If you created your project with &lt;code&gt;create-next-app&lt;/code&gt;, you would have this component too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iKylhky4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zff6ioush0ai4q890mql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iKylhky4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zff6ioush0ai4q890mql.png" alt="_app.js component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating a component that wraps around &lt;code&gt;&amp;lt;Component /&amp;gt;&lt;/code&gt; will get the job done.&lt;/p&gt;

&lt;h2&gt;
  
  
  The wrapping component
&lt;/h2&gt;

&lt;p&gt;Putting any component in the &lt;code&gt;pages&lt;/code&gt; folder will create a new page. This component will not be a separate page on its own. So, create a new folder named &lt;code&gt;components&lt;/code&gt; at the root of the project. Create a new file in it - &lt;code&gt;Layout.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ydNJXMdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1jbknv7im8dgpjod602.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ydNJXMdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1jbknv7im8dgpjod602.png" alt="Project folder structure after creating components folder with wrapper component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Writing a simple header like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fG-fsLjv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvw7kvn0alayl893t8ej.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fG-fsLjv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvw7kvn0alayl893t8ej.png" alt="Wrapper component - Layout.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and wrapping it around &lt;code&gt;&amp;lt;Component /&amp;gt;&lt;/code&gt; in &lt;code&gt;_app.js&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AYPFfMJS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tib3u0r1yaf3lspovu61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AYPFfMJS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tib3u0r1yaf3lspovu61.png" alt="_app.js component after using the Layout.js wrapper component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it should show up on all pages. Run the project:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is a CodeSandbox. I added some styles to &lt;code&gt;Layout.js&lt;/code&gt; and also some nav links. As you can see, the header (along with the newly added nav links or navbar) is shown on both pages.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/efrif"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Make a simple React app with Webpack - An easy practical guide</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Sun, 11 Apr 2021 13:26:59 +0000</pubDate>
      <link>https://dev.to/napoleon039/make-a-simple-react-app-with-webpack-an-easy-practical-guide-3nb3</link>
      <guid>https://dev.to/napoleon039/make-a-simple-react-app-with-webpack-an-easy-practical-guide-3nb3</guid>
      <description>&lt;p&gt;Create React App is the first choice of most, if not all, React developers. It creates a React project for us and only requires a few commands. Its simplicity and quick nature make it a favorite among beginners as well. But, there are also ways to create a React app without it.&lt;/p&gt;

&lt;p&gt;One of these ways is using a module bundler like Webpack and a compiler like Babel.&lt;/p&gt;

&lt;p&gt;By the end of this article, you will have your very own React app without using &lt;code&gt;create-react-app&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependencies and project structure
&lt;/h2&gt;

&lt;p&gt;Since this React app will use the Webpack module bundler, we need to install quite a few dependencies. These dependencies are required by Webpack so it can detect and work with the various file types.&lt;/p&gt;

&lt;p&gt;Here's what we need to install:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WY7qNNBG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxuk9s3coubsbyngjs6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WY7qNNBG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hxuk9s3coubsbyngjs6j.png" alt="Installing react"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These two packages are the dependencies we need for our simple app. If your project needs any other packages, you may install those. &lt;/p&gt;

&lt;p&gt;There are close to 10 devDependencies, so let's install them in groups and I'll explain what each dependency is for. First comes webpack:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GDv5cp2r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gm5gclxvv2q6z6nbaajj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GDv5cp2r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gm5gclxvv2q6z6nbaajj.png" alt="Installing webpack and webpack server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;webpack&lt;/code&gt;: Installs the webpack module bundler&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;webpack-cli&lt;/code&gt;: Offers a variety of commands that make it easier to work with webpack on the command line&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;webpack-dev-server&lt;/code&gt;: Allows us to use a simple web server with hot reload&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Next, we'll install Babel:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nE2fecnH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grf4bb4s3tzlnqdizrvr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nE2fecnH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grf4bb4s3tzlnqdizrvr.png" alt="Installing Babel, some presets and a loader"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@babel/core&lt;/code&gt;: Core package for the Babel compiler&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@babel/preset-env&lt;/code&gt;: A smart preset that allows us to use the latest JavaScript syntax&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@babel/preset-react&lt;/code&gt;: As the name suggests, it transpiles React code to plain JavaScript&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;babel-loader&lt;/code&gt;: A plugin that enables Webpack to work with Babel and its presets&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The remaining devDependencies are for CSS and HTML:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLMnGvvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wqsfd230ub9rv2rpou9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZLMnGvvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8wqsfd230ub9rv2rpou9.png" alt="Installing loaders for CSS and a plugin for HTML"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;style-loader&lt;/code&gt;, &lt;code&gt;css-loader&lt;/code&gt;: Required so that webpack can detect &lt;code&gt;.css&lt;/code&gt; files&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;html-webpack-plugin&lt;/code&gt;: Generates an HTML file that includes all your Webpack bundles via &lt;code&gt;script&lt;/code&gt; tags&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Everything a basic React project needs is installed. Now create these folders and file in the project's root:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;code&gt;public&lt;/code&gt; folder for the HTML file and assets (images, fonts, etc.)&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;src&lt;/code&gt; folder for &lt;code&gt;.js&lt;/code&gt; files and React components&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;babel.config.json&lt;/code&gt; Babel configuration file&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At this point, your project folder should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E8X0tWra--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fau70gxxufbz49ys0v4b.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E8X0tWra--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fau70gxxufbz49ys0v4b.JPG" alt="Folder structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Webpack and Babel config
&lt;/h2&gt;

&lt;p&gt;Let's add the presets in the Babel config file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GxuVC6Y7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhrtljke6m69muod5bal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GxuVC6Y7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dhrtljke6m69muod5bal.png" alt="Babel config file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is recommended to have two separate config files for Webpack - one for development and one for production. Although both files will have the same configuration for loaders and any plugins, there are slight differences. This is what a config file for development looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1yDefgqk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8zfbvnmd6xv06c1bqf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1yDefgqk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8zfbvnmd6xv06c1bqf1.png" alt="Webpack development mode config file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we first declare the &lt;code&gt;mode&lt;/code&gt; configuration option. With this, Webpack can use its built-in optimizations accordingly. Next, &lt;code&gt;module.rules&lt;/code&gt; is an array containing 3 objects:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This rule tells Webpack to look for files ending in &lt;code&gt;.js&lt;/code&gt; and use Babel to compile them. &lt;code&gt;babel-loader&lt;/code&gt; helps Webpack work with Babel.&lt;/li&gt;
&lt;li&gt;The second rule tells Webpack to look for files ending in &lt;code&gt;.css&lt;/code&gt; and make sense of them with the help of two loaders - &lt;code&gt;style-loader&lt;/code&gt; and &lt;code&gt;css-loader&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The last rule helps Webpack recognize image files. There is no need to install any external loader for this.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: According to the Webpack docs, &lt;code&gt;style-loader&lt;/code&gt; and &lt;code&gt;css-loader&lt;/code&gt; need to be used in the exact same order as in this config file or it won't work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After loaders, we have two plugins. When Webpack bundles all JavaScript, CSS, and other necessary files, it also creates an HTML file. This is where we use the &lt;code&gt;html-webpack-plugin&lt;/code&gt;. This plugin tells Webpack to use our HTML file as a template and inject the compiled bundles into it.&lt;/p&gt;

&lt;p&gt;So, instead of creating its own HTML file, Webpack instead uses &lt;em&gt;our&lt;/em&gt; HTML file - &lt;code&gt;public/index.html&lt;/code&gt; and adds the bundled files to it via &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags.&lt;/p&gt;

&lt;p&gt;The other plugin we use is &lt;code&gt;HotModuleReplacementPlugin&lt;/code&gt; which comes with the webpack package and is used for hot reloading our app in development mode.&lt;/p&gt;

&lt;p&gt;Finally, the &lt;code&gt;devServer&lt;/code&gt; object contains options used by &lt;code&gt;webpack-dev-server&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's take a look at the production version of the configuration file:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YCdH2FlI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whm4q3s5t5vkce50j9k2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YCdH2FlI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whm4q3s5t5vkce50j9k2.png" alt="Webpack production mode config file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not much has changed except that we no longer need &lt;code&gt;HotModuleReplacementPlugin&lt;/code&gt; and &lt;code&gt;devServer&lt;/code&gt; since they will not be used in production. The &lt;code&gt;mode&lt;/code&gt; option has also been set accordingly. You might have noticed the changes in &lt;code&gt;output&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;output.filename&lt;/code&gt; has a value that we haven't seen before. The values in square brackets are tokens. The &lt;code&gt;[name]&lt;/code&gt; token allows Webpack to name files differently if we use code-splitting. &lt;code&gt;[contenthash]&lt;/code&gt; is used so that the bundle file name changes when its content changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the app
&lt;/h2&gt;

&lt;p&gt;Our app is almost ready. I have a simple &lt;code&gt;App.js&lt;/code&gt; component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aI5_l7bL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlbntbk3wmwq12lnqb6g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aI5_l7bL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlbntbk3wmwq12lnqb6g.png" alt="Simple App component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The app looks like this now:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NEKGrH7B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rupfvcwip7fzozxrz4it.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NEKGrH7B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rupfvcwip7fzozxrz4it.JPG" alt="Folder structure after adding Webpack config files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we need to add scripts to run our app:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Em5Kuxa6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mox77xufnpdd270uuv7p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Em5Kuxa6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mox77xufnpdd270uuv7p.png" alt="Build and run scripts that use both Webpack config files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;serve&lt;/code&gt; option Webpack uses &lt;code&gt;webpack-dev-server&lt;/code&gt; to create a web server. Let's run our app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sPdphrWD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6hmewbev8dpvnxiu5dc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sPdphrWD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b6hmewbev8dpvnxiu5dc.png" alt="Running the app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;https://localhost:8080&lt;/code&gt; in your browser and your app should be running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;This is a basic React app where we manually configure Webpack. I hope this helped you to understand a bit about the various plugins and loaders Webpack requires and what its configuration files look like. The Webpack documentation is worth reading. It was the major resource I used for this article.&lt;/p&gt;

&lt;p&gt;In comparison, &lt;a href="https://parceljs.org/getting_started.html"&gt;Parcel&lt;/a&gt; uses very few dependencies. I wrote another article about creating &lt;a href="https://dev.to/napoleon039/how-to-create-a-minimal-react-and-parcel-app-in-5-steps-4hj7"&gt;a React app with Parcel&lt;/a&gt;. That said, both have their own pros and cons. &lt;/p&gt;

&lt;p&gt;Here are links to some additional resources:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://webpack.js.org/configuration/"&gt;Webpack documentation&lt;/a&gt; on config files and the various options a config file can have&lt;/li&gt;
&lt;li&gt;Webpack documentation on &lt;a href="https://webpack.js.org/guides/asset-management/#loading-css"&gt;loading CSS&lt;/a&gt; and other assets&lt;/li&gt;
&lt;li&gt;Webpack documentation on &lt;a href="https://webpack.js.org/guides/development/#using-webpack-dev-server"&gt;webpack-dev-server&lt;/a&gt; and &lt;a href="https://webpack.js.org/plugins/html-webpack-plugin/#basic-usage"&gt;html-webpack-plugin&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;An article from &lt;a href="https://twitter.com/carlrippon"&gt;Carl Rippon&lt;/a&gt; on creating a &lt;a href="https://www.carlrippon.com/creating-react-app-with-typescript-eslint-with-webpack5/"&gt;React app&lt;/a&gt; that uses TypeScript and ESLint with Webpack 5&lt;/li&gt;
&lt;li&gt;Babel documentation on its &lt;a href="https://babeljs.io/docs/en/configuration"&gt;config files&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webpack</category>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How do closures make private variables possible in JavaScript?</title>
      <dc:creator>Nihar Raote</dc:creator>
      <pubDate>Sat, 06 Mar 2021 12:22:40 +0000</pubDate>
      <link>https://dev.to/napoleon039/how-do-closures-make-private-variables-possible-in-javascript-66o</link>
      <guid>https://dev.to/napoleon039/how-do-closures-make-private-variables-possible-in-javascript-66o</guid>
      <description>&lt;p&gt;Being able to implement private variables and methods is very helpful for restricting access to code. By making a variable private it limits the amount of code that can mutate it. There are several ways to implement private variables and methods in JavaScript. One of such ways is by using closures.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a closure?
&lt;/h2&gt;

&lt;p&gt;A closure is the combination of a function bundled with its surrounding state. This means that we can access the scope in which the function is defined, from inside that function. Let's look at a simple example:&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%2Fkn1vxndikkpu2693yjrl.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%2Fkn1vxndikkpu2693yjrl.png" alt="An example of a closure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have an outer function &lt;code&gt;add&lt;/code&gt; and an inner function &lt;code&gt;addFive&lt;/code&gt;. Assigning &lt;code&gt;add&lt;/code&gt; to a variable will return the inner function &lt;code&gt;addFive&lt;/code&gt;.&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%2Ffrbd0oxzute8ypksnz6c.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%2Ffrbd0oxzute8ypksnz6c.png" alt="Executing closure example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Invoking the variable and passing a number will execute &lt;code&gt;addFive&lt;/code&gt;. At first, it seems accessing &lt;code&gt;x&lt;/code&gt; should not be possible since &lt;code&gt;x&lt;/code&gt; belongs to the scope of the outer function which has already finished executing. But, because the function &lt;code&gt;addFive&lt;/code&gt; forms a closure, it also has access to the environment or scope in which it was declared (also known as the lexical environment). &lt;/p&gt;

&lt;p&gt;If a function forms a closure, it can access variables and functions declared in the parent/outer function even after that parent/outer function finishes executing. This principle allows us to implement private variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing private variables
&lt;/h2&gt;

&lt;p&gt;In JavaScript, private variables are not natively available. But knowing how closures work, we can emulate them.&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%2Fkr2816q80zh2c9mrxsg1.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%2Fkr2816q80zh2c9mrxsg1.png" alt="Implementing private variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This example applies the concept we looked at in the previous section. An IIFE(Immediately Invoked Function Expression) is assigned to the constant &lt;code&gt;Door&lt;/code&gt;. The IIFE executes immediately and returns an object with two methods. This returned object has a function and a variable in its lexical environment.&lt;/p&gt;

&lt;p&gt;The IIFE finishes executing, so the variable and function are both inaccessible from the outside. Only the two methods inside the returned object can access them because they form closures.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
