<?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: Apollo Fredrick</title>
    <description>The latest articles on Forem by Apollo Fredrick (@teddapollo).</description>
    <link>https://forem.com/teddapollo</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%2F1223845%2F0fe7a838-9206-48eb-8055-500e314ccd31.jpg</url>
      <title>Forem: Apollo Fredrick</title>
      <link>https://forem.com/teddapollo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/teddapollo"/>
    <language>en</language>
    <item>
      <title>A beginners guide to Dockerizing a React application</title>
      <dc:creator>Apollo Fredrick</dc:creator>
      <pubDate>Sun, 06 Jul 2025 20:01:07 +0000</pubDate>
      <link>https://forem.com/teddapollo/a-beginners-guide-to-dockerizing-a-react-application-4g8c</link>
      <guid>https://forem.com/teddapollo/a-beginners-guide-to-dockerizing-a-react-application-4g8c</guid>
      <description>&lt;p&gt;Containerization is a powerful tool in modern web development as it allows consistency in running applications in all platforms. Docker is a popular containerization tool as it offers consistency, scalability, and ease of deployment.&lt;/p&gt;

&lt;p&gt;In this tutorial, we are going to perform the following tasks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Creating a basic React/Vite app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dockerizing the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Building and testing the Docker image locally&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pushing the image to Docker Hub&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node.js(version 18 or higher)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker Desktop installed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker Hub account&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating a React/Vite project
&lt;/h2&gt;

&lt;p&gt;Open the project folder in your code editor and run the following commands in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest react-docker --template react 
cd react-docker
npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start the development server to ensure that the app works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a Dockerfile
&lt;/h2&gt;

&lt;p&gt;A Dockerfile is a text file containing a set of instructions used to automate the creation of a Docker image. It provides a blueprint for building an isolated environment, essentially packaging an application and its dependencies into a self-contained unit. &lt;br&gt;
A Dockerfile consists of a series of commands (instructions) that tell Docker how to build the image. &lt;br&gt;
Create a file called &lt;code&gt;Dockerfile&lt;/code&gt; in the root directory(do not add any extension in the name)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM node:18-alpine AS builder
# Use a lightweight Node.js image for building the application
# Use the official Node.js image as the base image

WORKDIR /app
# Set the working directory inside the container

COPY package*.json ./
# Copy package.json and package-lock.json to the working directory
# This allows us to install dependencies before copying the rest of the application code

RUN npm install
# Install the dependencies defined in package.json
# This will create a node_modules directory with all the dependencies

COPY . .
# Copy the rest of the application code to the working directory
# This includes the source code, configuration files, etc.

RUN npm run build
# Build the application using the build script defined in package.json
# This will create a production-ready build of the application

RUN npm install -g serve
# Install the serve package globally to serve the built application

EXPOSE 5173
# Expose port 5173 for the application to be accessible from outside the container

CMD ["serve", "-s", "dist", "-l", "5173"]
# Start the application using serve, serving the built files from the dist directory on port 5173
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the .dockerignore file
&lt;/h2&gt;

&lt;p&gt;The .dockerignore file helps avoid sending unwanted files and directories to Docker Hub.&lt;br&gt;
Create the &lt;code&gt;.dockerignore&lt;/code&gt; file in the root directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dist
node_modules
env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Building the image
&lt;/h2&gt;

&lt;p&gt;Ensure that the Docker Desktop is running.&lt;br&gt;
Run the following command in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t react-docker .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing the image locally
&lt;/h2&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 5173:5173 react-docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visit &lt;code&gt;http:localhost:5173&lt;/code&gt; to see your app running&lt;/p&gt;

&lt;h2&gt;
  
  
  Pushing to Docker Hub
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Log in to Docker Hub:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker login -u &amp;lt;username&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Tag your image with your Docker Hub repository:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker tag react-docker username/react-docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Push to Docker Hub
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker push username/react-docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Verify your image in Docker Hub&lt;br&gt;
Open the browser and log in to your Docker Hub account. &lt;br&gt;
Your image should be listed in the repositories.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pull and run the image remotely&lt;br&gt;
You can pull the image and run it in a separate machine&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker pull username/react-docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 5173:5173 username/react-docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;By containerizing your React/Vite application, you've created a portable, scalable deployment artifact. Docker Hub serves as your distribution hub, enabling seamless deployment to any environment that supports Docker. This foundation unlocks advanced deployment strategies like rolling updates, blue-green deployments, and autoscaling.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>react</category>
      <category>devops</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React API calls made simple with Axios</title>
      <dc:creator>Apollo Fredrick</dc:creator>
      <pubDate>Tue, 23 Jan 2024 18:48:23 +0000</pubDate>
      <link>https://forem.com/teddapollo/react-api-calls-made-simple-with-axios-5c2e</link>
      <guid>https://forem.com/teddapollo/react-api-calls-made-simple-with-axios-5c2e</guid>
      <description>&lt;p&gt;React is a JavaScript library for building user interfaces. Often you will want to fetch data from an API and display it in your React application.&lt;br&gt;
Axios is a promise-based HTTP client that makes it easy to fetch data from APIs.&lt;br&gt;
In this tutorial I will show you the simplest way to fetch API data and display it in your website.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;You should have a basic knowledge of React&lt;/p&gt;
&lt;h2&gt;
  
  
  Install Axios
&lt;/h2&gt;

&lt;p&gt;Install Axios in your project folder using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Import Axios
&lt;/h2&gt;

&lt;p&gt;Import Axios in the component which you want to make API calls in as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import axios from 'axios';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Making a GET Request
&lt;/h2&gt;

&lt;p&gt;To make a GET request, use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.get(url)
  .then(response =&amp;gt; {
    // Handle the successful response here
    console.log(response.data);
  })
  .catch(error =&amp;gt; {
    // Handle errors here
    console.error('Error fetching data:', error);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Handling Asynchronous Operations
&lt;/h2&gt;

&lt;p&gt;To handle asynchronous functions we use 'async/await' since API calls are asynchronous operations and React ensures that the UI remains responsive during these calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fetchData = async() =&amp;gt;{
      try {
        const response = await axios.get(url)
        setData([response.data])
      } catch (error) {
        console.log(error)
      }
    }
    fetchData()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Integrate with React Components
&lt;/h2&gt;

&lt;p&gt;Integrate your knowledge of data fetching in your React components as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios'
import React, {useState, useEffect} from 'react'

const App = () =&amp;gt; {
  const [data, setData] = useState([])
  const url = "https://api.chucknorris.io/jokes/random"

  useEffect(() =&amp;gt; {
    const fetchData = async() =&amp;gt;{
      try {
        const response = await axios.get(url)
        setData([response.data.value])
      } catch (error) {
        console.log(error)
      }
    }
    fetchData()
  }, [])
  return (
    &amp;lt;ul&amp;gt;
      {data.map((item, index) =&amp;gt; (
        &amp;lt;li key={index}&amp;gt;{item}&amp;lt;/li&amp;gt;
      ))}
  &amp;lt;/ul&amp;gt;
  )
}

export default App

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

&lt;/div&gt;



&lt;p&gt;We use the useEffect hook to call the API when the component mounts.&lt;br&gt;
Axios makes the Get request and returns a promise. When the promise resolves, we set the state using setData.&lt;br&gt;
We finally use the map function to loop the data and display it on the page.&lt;br&gt;
Remove React.StrictMode in your index.js file(If you are using Create-React-App) or main.js file(If you are using Vite) to prevent the useEfect hook from running twice.&lt;br&gt;
Refresh the page to get more Chuck Norris jokes.&lt;/p&gt;

</description>
      <category>react</category>
      <category>api</category>
      <category>learning</category>
      <category>newbie</category>
    </item>
    <item>
      <title>React API calls made simple with Axios</title>
      <dc:creator>Apollo Fredrick</dc:creator>
      <pubDate>Tue, 23 Jan 2024 18:45:01 +0000</pubDate>
      <link>https://forem.com/teddapollo/react-api-calls-made-simple-with-axios-2kop</link>
      <guid>https://forem.com/teddapollo/react-api-calls-made-simple-with-axios-2kop</guid>
      <description>&lt;p&gt;React is a JavaScript library for building user interfaces. Often you will want to fetch data from an API and display it in your React application.&lt;br&gt;
Axios is a promise-based HTTP client that makes it easy to fetch data from APIs.&lt;br&gt;
In this tutorial I will show you the simplest way to fetch API data and display it in your website.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;You should have a basic knowledge of React&lt;/p&gt;
&lt;h2&gt;
  
  
  Install Axios
&lt;/h2&gt;

&lt;p&gt;Install Axios in your project folder using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Import Axios
&lt;/h2&gt;

&lt;p&gt;Import Axios in the component which you want to make API calls in as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useEffect, useState } from 'react';
import axios from 'axios';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Making a GET Request
&lt;/h2&gt;

&lt;p&gt;To make a GET request, use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.get(url)
  .then(response =&amp;gt; {
    // Handle the successful response here
    console.log(response.data);
  })
  .catch(error =&amp;gt; {
    // Handle errors here
    console.error('Error fetching data:', error);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Handling Asynchronous Operations
&lt;/h2&gt;

&lt;p&gt;To handle asynchronous functions we use 'async/await' since API calls are asynchronous operations and React ensures that the UI remains responsive during these calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fetchData = async() =&amp;gt;{
      try {
        const response = await axios.get(url)
        setData([response.data])
      } catch (error) {
        console.log(error)
      }
    }
    fetchData()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Integrate with React Components
&lt;/h2&gt;

&lt;p&gt;Integrate your knowledge of data fetching in your React components as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios'
import React, {useState, useEffect} from 'react'

const App = () =&amp;gt; {
  const [data, setData] = useState([])
  const url = "https://api.chucknorris.io/jokes/random"

  useEffect(() =&amp;gt; {
    const fetchData = async() =&amp;gt;{
      try {
        const response = await axios.get(url)
        setData([response.data.value])
      } catch (error) {
        console.log(error)
      }
    }
    fetchData()
  }, [])
  return (
    &amp;lt;ul&amp;gt;
      {data.map((item, index) =&amp;gt; (
        &amp;lt;li key={index}&amp;gt;{item}&amp;lt;/li&amp;gt;
      ))}
  &amp;lt;/ul&amp;gt;
  )
}

export default App

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

&lt;/div&gt;



&lt;p&gt;We use the useEffect hook to call the API when the component mounts.&lt;br&gt;
Axios makes the Get request and returns a promise. When the promise resolves, we set the state using setData.&lt;br&gt;
We finally use the map function to loop the data and display it on the page.&lt;br&gt;
Remove React.StrictMode in your index.js file(If you are using Create-React-App) or main.js file(If you are using Vite) to prevent the useEfect hook from running twice.&lt;br&gt;
Refresh the page to get more Chuck Norris jokes.&lt;/p&gt;

</description>
      <category>react</category>
      <category>api</category>
      <category>learning</category>
      <category>newbie</category>
    </item>
    <item>
      <title>Lazy Loading in React</title>
      <dc:creator>Apollo Fredrick</dc:creator>
      <pubDate>Tue, 12 Dec 2023 13:42:41 +0000</pubDate>
      <link>https://forem.com/teddapollo/lazy-loading-in-react-4a6f</link>
      <guid>https://forem.com/teddapollo/lazy-loading-in-react-4a6f</guid>
      <description>&lt;h2&gt;
  
  
  What is Lazy Loading?
&lt;/h2&gt;

&lt;p&gt;Lazy loading is a technique for asynchronously loading components or resources only when they are needed. This means that instead of downloading the entire application at once, which can lead to slow loading times, especially on slower internet connections, the application only downloads the components that are currently visible to the user. This significantly improves the initial page load and overall user experience.&lt;/p&gt;

&lt;p&gt;In this tutorial we are going to learn how to implement Lazy Loading in React.&lt;/p&gt;

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

&lt;p&gt;You need to have a basic knowledge of React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the project
&lt;/h2&gt;

&lt;p&gt;We are going to create our React application using Vite and not Create-React-App since Vite is fast.&lt;br&gt;
Run the following command in your project folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest lazy-loading
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Select the framework as React and language as Javascript then run the following command to install the dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating components
&lt;/h2&gt;

&lt;p&gt;We are going to create two components, one that will be loaded immediately(Home.jsx) and another that will be loaded lazily(About.jsx)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Home.jsx
import React from 'react';

const Home = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Welcome to Lazy Loading Demo&amp;lt;/h1&amp;gt;
      &amp;lt;h2&amp;gt;This component is loaded immediately&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Home;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//About.jsx
import React from 'react';

const About = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;This component was lazily loaded!&amp;lt;/h2&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default About;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install react-router-dom as we only want to load the lazy component when we navigate to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a navigation component with links to the components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { Link } from 'react-router-dom'

const Navbar = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;nav&amp;gt;
        &amp;lt;ul&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/home"&amp;gt;Home&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;Link to="/about"&amp;gt;About&amp;lt;/Link&amp;gt;
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
      &amp;lt;/nav&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Navbar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify the App.jsx component as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// App.js
import React, {lazy, Suspense} from 'react'
import {Routes, Route} from 'react-router-dom'
import Navbar from './Navbar'
import Home from './Home'
const About = lazy(() =&amp;gt; import('./About'));

function App() {
  return (
    &amp;lt;&amp;gt;
    &amp;lt;Navbar/&amp;gt;
    &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/home" element={&amp;lt;Home/&amp;gt;} /&amp;gt;
        &amp;lt;Route
          path="/about" 
          element={
          &amp;lt;Suspense fallback='Loading...'&amp;gt;
            &amp;lt;About/&amp;gt;
          &amp;lt;/Suspense&amp;gt; }/&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/&amp;gt;
  )
}

export default App
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrap the App component in BrowserRouter in the main.jsx as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import { BrowserRouter } from 'react-router-dom'

ReactDOM.createRoot(document.getElementById('root')).render(
  &amp;lt;BrowserRouter&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/BrowserRouter&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Run your React application using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Home component is loaded immediately when the root route (/home) is accessed. The About component is loaded lazily when navigating to the /about route.&lt;/p&gt;

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

&lt;p&gt;Lazy loading with react-router-dom is a powerful technique to improve the initial load time of your React applications. By dynamically loading components only when they are needed, you can enhance the user experience and optimize the performance of your application. Consider applying lazy loading to larger components or those that are not crucial for the initial view, ensuring a smoother user interaction.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting started with dotenv in Node.js</title>
      <dc:creator>Apollo Fredrick</dc:creator>
      <pubDate>Sat, 02 Dec 2023 10:05:31 +0000</pubDate>
      <link>https://forem.com/teddapollo/getting-started-with-dotenv-in-nodejs-341h</link>
      <guid>https://forem.com/teddapollo/getting-started-with-dotenv-in-nodejs-341h</guid>
      <description>&lt;p&gt;Node.js applications often require configuration parameters like API keys, database URLs. Hardcoding these values directly in your code can be dangerous especially when sharing your code. The &lt;strong&gt;'dotenv'&lt;/strong&gt; package in Node.js provides a simple and effective solution for managing environmental variables in your applications.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Dotenv&lt;/strong&gt; is a simple and popular module for loading environmental variables from a &lt;strong&gt;.env&lt;/strong&gt; file into a Node.js application. The purpose of using &lt;strong&gt;'dotenv'&lt;/strong&gt; is to keep sensitive information separate from your codebase and to make it easy to configure your application in different environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing dotenv
&lt;/h2&gt;

&lt;p&gt;To get started, you need to install the &lt;strong&gt;'dotenv'&lt;/strong&gt; package in your Node.js project. You can do so by using npm or yarn depending on your package manager.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install dotenv
yarn add dotenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a .env file
&lt;/h2&gt;

&lt;p&gt;Once &lt;strong&gt;'dotenv'&lt;/strong&gt; is installed, create a file named &lt;strong&gt;.env&lt;/strong&gt; in the root directory of your project. This is where you will store your environmental variables in simple key=value formats.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGODB_URL = "mongodb+srv://test:&amp;lt;password&amp;gt;@my-project.yjjwl8u.mongodb.net/?retryWrites=true&amp;amp;w=majority"
PORT = 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing Environmental Variables
&lt;/h2&gt;

&lt;p&gt;Now in the &lt;strong&gt;index.js&lt;/strong&gt; file, require and load &lt;strong&gt;dotenv&lt;/strong&gt; at the top.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require('dotenv').config();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line loads the &lt;strong&gt;dotenv&lt;/strong&gt; module and calls the config method which reads the variables from the &lt;strong&gt;.env&lt;/strong&gt; file and adds them to &lt;strong&gt;process.env&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example in code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express')
const mongoose = require('mongoose')
const dotenv = require('dotenv').config()

const app = express()

mongoose.connect(process.env.MONGODB_URL)
    .then(() =&amp;gt;{
        console.log("Database connected successfully")
    })
    .catch(() =&amp;gt; {
        console.log("Database connection failed")
    })

app.listen(process.env.PORT, () =&amp;gt;{
    console.log(`Server running on port ${port}`)
})

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

&lt;/div&gt;



&lt;p&gt;The code above connects to MongoDB database and creates a simple web server.&lt;/p&gt;

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

&lt;p&gt;Keep the &lt;strong&gt;.env&lt;/strong&gt; in &lt;strong&gt;.gitignore.&lt;/strong&gt; This ensures that the &lt;strong&gt;.env&lt;/strong&gt; file is listed in the project's &lt;strong&gt;.gitignore&lt;/strong&gt; file. This prevents sensitive information from being committed to version control.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>node</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
