<?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: Victor Webster</title>
    <description>The latest articles on DEV Community by Victor Webster (@chukwurahvictor).</description>
    <link>https://dev.to/chukwurahvictor</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%2F308113%2F7fa6f952-46fe-452e-a871-25dbf8a3fb56.JPG</url>
      <title>DEV Community: Victor Webster</title>
      <link>https://dev.to/chukwurahvictor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chukwurahvictor"/>
    <language>en</language>
    <item>
      <title>Build a simple react application to implement the contextApi (useContext)</title>
      <dc:creator>Victor Webster</dc:creator>
      <pubDate>Tue, 10 Jan 2023 22:08:34 +0000</pubDate>
      <link>https://dev.to/chukwurahvictor/build-a-simple-react-application-to-implement-the-contextapi-usecontext-2dae</link>
      <guid>https://dev.to/chukwurahvictor/build-a-simple-react-application-to-implement-the-contextapi-usecontext-2dae</guid>
      <description>&lt;p&gt;Let's build a simple react application to implement the contextApi.&lt;/p&gt;

&lt;p&gt;Create a new react app using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following dependencies will be required for the application:&lt;br&gt;
&lt;strong&gt;react-router-dom&lt;/strong&gt;: To help with routing between pages in our application&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;react-error-boundary&lt;/strong&gt;: To help with implementing error boundary&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;tailwindcss&lt;/strong&gt;: A low utility css framework to help with styling our application&lt;/p&gt;

&lt;p&gt;To install these dependencies, use the 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 react-router-dom react-error-boundary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's talk briefly about the project structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fzx9zoj8ugmc608gsbvc2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzx9zoj8ugmc608gsbvc2.PNG" alt="project-structure" width="449" height="539"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Components&lt;/strong&gt;: This folder will hold all our reusable components like Navbar, Sidebar, etc.&lt;br&gt;
&lt;strong&gt;Context&lt;/strong&gt;: This folder will house our userContext configuration file.&lt;br&gt;
&lt;strong&gt;Hooks&lt;/strong&gt;: This will hold our custom hooks file.&lt;br&gt;
&lt;strong&gt;Pages&lt;/strong&gt;: The different pages in our application will be here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Build the Navbar component&lt;/strong&gt;&lt;br&gt;
Inside the components folder, create a Navbar.jsx file and put this code.&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, { useState, useContext } from 'react';
import { UserContext } from "../context/UserContext";

const Navbar = () =&amp;gt; {
  const [navbar, setNavbar] = useState(false);
  const { isAuth, userInfo, logout } = useContext(UserContext);

  const onLogout = (e) =&amp;gt; {
    e.preventDefault();
    logout();
  };

   return (
     &amp;lt;nav className="w-full bg-blue-800 shadow"&amp;gt;
       &amp;lt;div className="justify-between px-4 mx-auto lg:max-w-7xl md:items-center md:flex md:px-8"&amp;gt;
         &amp;lt;div&amp;gt;
           &amp;lt;div className="flex items-center justify-between py-3 md:py-5 md:block"&amp;gt;
             &amp;lt;a href="/"&amp;gt;
               &amp;lt;h2 className="text-2xl font-bold text-white"&amp;gt;AltSchool&amp;lt;/h2&amp;gt;
             &amp;lt;/a&amp;gt;
             &amp;lt;div className="md:hidden"&amp;gt;
               &amp;lt;button
                 className="p-2 text-gray-700 rounded-md outline-none focus:border-gray-400 focus:border"
                 onClick={() =&amp;gt; setNavbar(!navbar)}
               &amp;gt;
                 {navbar ? (
                   &amp;lt;svg
                     xmlns="http://www.w3.org/2000/svg"
                     className="w-6 h-6 text-white"
                     viewBox="0 0 20 20"
                     fill="currentColor"
                   &amp;gt;
                     &amp;lt;path
                       fillRule="evenodd"
                       d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
                       clipRule="evenodd"
                     /&amp;gt;
                   &amp;lt;/svg&amp;gt;
                 ) : (
                   &amp;lt;svg
                     xmlns="http://www.w3.org/2000/svg"
                     className="w-6 h-6 text-white"
                     fill="none"
                     viewBox="0 0 24 24"
                     stroke="currentColor"
                     strokeWidth={2}
                   &amp;gt;
                     &amp;lt;path
                       strokeLinecap="round"
                       strokeLinejoin="round"
                       d="M4 6h16M4 12h16M4 18h16"
                     /&amp;gt;
                   &amp;lt;/svg&amp;gt;
                 )}
               &amp;lt;/button&amp;gt;
             &amp;lt;/div&amp;gt;
           &amp;lt;/div&amp;gt;
         &amp;lt;/div&amp;gt;
         &amp;lt;div&amp;gt;
           &amp;lt;div
             className={`flex-1 justify-self-center pb-3 mt-8 md:block md:pb-0 md:mt-0 ${
               navbar ? "block" : "hidden"
             }`}
           &amp;gt;
             &amp;lt;ul className="items-center justify-center space-y-8 md:flex md:space-x-6 md:space-y-0"&amp;gt;
               &amp;lt;li className="text-white hover:text-indigo-200"&amp;gt;
                 &amp;lt;a href="/"&amp;gt;Home&amp;lt;/a&amp;gt;
               &amp;lt;/li&amp;gt;
               &amp;lt;li className="text-white hover:text-indigo-200"&amp;gt;
                 &amp;lt;a href="/about"&amp;gt;About&amp;lt;/a&amp;gt;
               &amp;lt;/li&amp;gt;
               &amp;lt;li className="text-white hover:text-indigo-200"&amp;gt;
                 &amp;lt;a href="/contact"&amp;gt;Contact&amp;lt;/a&amp;gt;
               &amp;lt;/li&amp;gt;
             &amp;lt;/ul&amp;gt;

             &amp;lt;div className="mt-3 space-y-2 lg:hidden md:hidden"&amp;gt;
               {!isAuth ? (
                 &amp;lt;a
                   href="/login"
                   className="inline-block w-full px-4 py-2 text-center text-white bg-gray-600 rounded-md shadow hover:bg-gray-800"
                 &amp;gt;
                   Sign in
                 &amp;lt;/a&amp;gt;
               ) : (
                 &amp;lt;&amp;gt;
                   &amp;lt;p className="inline-block w-full px-4 py-2 text-center text-white"&amp;gt;
                     Welcome, {userInfo?.username}
                   &amp;lt;/p&amp;gt;
                   &amp;lt;button
                     onClick={onLogout}
                     className="inline-block w-full px-4 py-2 text-center text-white bg-gray-600 rounded-md shadow hover:bg-gray-800"
                   &amp;gt;
                     Logout
                   &amp;lt;/button&amp;gt;
                 &amp;lt;/&amp;gt;
               )}
             &amp;lt;/div&amp;gt;
           &amp;lt;/div&amp;gt;
         &amp;lt;/div&amp;gt;
         &amp;lt;div className="hidden space-x-2 md:flex"&amp;gt;
           {!isAuth ? (
             &amp;lt;a
               href="/login"
               className="px-4 py-2 text-white bg-gray-600 rounded-md shadow hover:bg-gray-800"
             &amp;gt;
               Sign in
             &amp;lt;/a&amp;gt;
           ) : (
             &amp;lt;&amp;gt;
               &amp;lt;div class="overflow-hidden relative w-10 h-10 bg-gray-100 rounded-full dark:bg-gray-600"&amp;gt;
                 &amp;lt;svg
                   class="absolute -left-1 w-12 h-12 text-gray-400"
                   fill="currentColor"
                   viewBox="0 0 20 20"
                   xmlns="http://www.w3.org/2000/svg"
                 &amp;gt;
                   &amp;lt;path
                     fill-rule="evenodd"
                     d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z"
                     clip-rule="evenodd"
                   &amp;gt;&amp;lt;/path&amp;gt;
                 &amp;lt;/svg&amp;gt;
               &amp;lt;/div&amp;gt;
               &amp;lt;div className="px-4 py-2 text-white"&amp;gt;
                 &amp;lt;p&amp;gt;Welcome, {userInfo?.username}&amp;lt;/p&amp;gt;
               &amp;lt;/div&amp;gt;
               &amp;lt;button
                 onClick={onLogout}
                 className="px-4 py-2 text-white bg-gray-600 rounded-md shadow hover:bg-gray-800"
               &amp;gt;
                 Logout
               &amp;lt;/button&amp;gt;
             &amp;lt;/&amp;gt;
           )}
         &amp;lt;/div&amp;gt;
       &amp;lt;/div&amp;gt;
     &amp;lt;/nav&amp;gt;
   );
   }

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

&lt;/div&gt;



&lt;p&gt;Inside of the App.js file, import the navbar component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Navbar from "./components/Navbar.jsx";

const App = () =&amp;gt; {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Navbar /&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Set up userContext&lt;/strong&gt;&lt;br&gt;
Inside the context folder, create a userContext.js file and put this code.&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, { createContext, useState, useEffect } from "react";

const initialState = {
  userInfo: null,
  isAuth: false,
};

export const UserContext = createContext(initialState);

export const UserContextProvider = ({ children }) =&amp;gt; {
  const [userInfo, setUserInfo] = useState(null);
  const [isAuth, setIsAuth] = useState(false);

  useEffect(() =&amp;gt; {
    const storedUser = localStorage.getItem("user");
    if (storedUser) {
      setUserInfo(JSON.parse(storedUser));
      setIsAuth(true);
    }
  }, []);

  const login = (username, password) =&amp;gt; {
    setIsAuth(true);
    setUserInfo({ username, password });
    localStorage.setItem("user", JSON.stringify({username, password}));
    localStorage.setItem("auth", "true");
  };

  const logout = () =&amp;gt; {
    fetch("/logout").then((res) =&amp;gt; {
      setIsAuth(false);
      setUserInfo(null);
      localStorage.removeItem("user");
      localStorage.removeItem("auth");
    });
  };

   const value = {
      userInfo,
      setUserInfo,
      isAuth,
      setIsAuth,
      login,
      logout,
   };

   return (
      &amp;lt;UserContext.Provider value={value}&amp;gt; {children} &amp;lt;/UserContext.Provider&amp;gt;
   );
};

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

&lt;/div&gt;



&lt;p&gt;We start by setting our default states for the user which is null for userInfo and false for isAuth. We can then use the createContext by react to create the userContext using the default states. Next, we create the contextProvider which takes a props of children and also create functions to handle login and logout in our application. When a user logs in, we update the user state and also store the updated user state in the localStorage to persist the details i.e it does not return to default on refresh.&lt;/p&gt;

&lt;p&gt;We should have something like this on our screen when we run our application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fgk3hbxwisyr79x310zza.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fgk3hbxwisyr79x310zza.PNG" alt="navbar" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set up Routing&lt;/strong&gt;&lt;br&gt;
Now, let's set up our routing. First, we build all the pages and components needed like our dashboard, 404, Login, About and Contact pages and import them into out App.js file then we set up error boundary using the react-error-boundary package we installed earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import { ErrorBoundary } from "react-error-boundary";

import Home from "./pages/Home";
import About from "./pages/About";
import Contact from "./pages/Contact";
import NotFound from "./pages/404"
import Dashboard from "./pages/Dashboard";
import Navbar from "./components/Navbar.jsx";
import Login from './pages/Login';

const ErrorFallback = ({ error }) =&amp;gt; {
  return (
    &amp;lt;div role="alert"&amp;gt;
      &amp;lt;p&amp;gt;Something went wrong:&amp;lt;/p&amp;gt;
      &amp;lt;pre style={{ color: "red" }}&amp;gt;{error.message}&amp;lt;/pre&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
const App = () =&amp;gt; {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;ErrorBoundary FallbackComponent={ErrorFallback}&amp;gt;
        &amp;lt;Navbar /&amp;gt;
        &amp;lt;Router&amp;gt;
          &amp;lt;Routes&amp;gt;
            &amp;lt;Route path="/" element={&amp;lt;Home /&amp;gt;} /&amp;gt;
            &amp;lt;Route path="/login" element={&amp;lt;Login /&amp;gt;} /&amp;gt;
            &amp;lt;Route path="/dashboard" element={&amp;lt;Dashboard /&amp;gt;}/&amp;gt;
            &amp;lt;Route path="/about" element={&amp;lt;About /&amp;gt;} /&amp;gt;
            &amp;lt;Route path="/contact" element={&amp;lt;Contact /&amp;gt;} /&amp;gt;
            &amp;lt;Route path="*" element={&amp;lt;NotFound /&amp;gt;} /&amp;gt;
          &amp;lt;/Routes&amp;gt;
        &amp;lt;/Router&amp;gt;
      &amp;lt;/ErrorBoundary&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Set up Custom hook&lt;/strong&gt;&lt;br&gt;
We will now set a custom hook to get the user from the userContext and can be used anywhere in our code. Inside the hooks folder, create a getUser.js file and add this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useContext } from 'react';
import { UserContext } from "../context/UserContext";

const useGetUser = () =&amp;gt; {
  const { userInfo, isAuth } = useContext(UserContext);
  return {userInfo, isAuth};
};

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

&lt;/div&gt;



&lt;p&gt;The useGetUser hook can now be used anywhere in our code to get the current logged in user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set up protected routing&lt;/strong&gt;&lt;br&gt;
The protected routing is used to protect our routes from being accessed by unauthorized users. So, a user has to be logged in before they can access a route that is protected.&lt;br&gt;
&lt;/p&gt;

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

export const ProtectedRoute = ({ children }) =&amp;gt; {
   const isAuth = localStorage.getItem("auth");
   if (!isAuth) {
     // user is not authenticated
     return &amp;lt;Navigate to="/login" /&amp;gt;;
   }
   return children;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import the protectedRoute into the App.js file and wrap the elements of the routes you want to protect like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            &amp;lt;Route
              path="/dashboard"
              element={
                &amp;lt;ProtectedRoute&amp;gt;
                  &amp;lt;Dashboard /&amp;gt;
                &amp;lt;/ProtectedRoute&amp;gt;
              }
            /&amp;gt;
            &amp;lt;Route
              path="/about"
              element={
                &amp;lt;ProtectedRoute&amp;gt;
                  &amp;lt;About /&amp;gt;
                &amp;lt;/ProtectedRoute&amp;gt;
              }
            /&amp;gt;
            &amp;lt;Route
              path="/contact"
              element={
                &amp;lt;ProtectedRoute&amp;gt;
                  &amp;lt;Contact /&amp;gt;
                &amp;lt;/ProtectedRoute&amp;gt;
              }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The project should look something like this at the end:&lt;/p&gt;

&lt;p&gt;When Unauthorized:&lt;br&gt;
&lt;a href="https://media2.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%2Fzuoi4ff0p411snrboq67.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fzuoi4ff0p411snrboq67.PNG" alt="Unauthorized" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Authorized:&lt;br&gt;
&lt;a href="https://media2.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%2F6ndnw0ql00lodgltlwys.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F6ndnw0ql00lodgltlwys.PNG" alt="Authorized" width="800" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The link to the complete project has been provided below as well as the project link and other important documentations for packages and dependencies used in this application. In case you encounter errors or have any questions, you can go through the links or ask me in the comment section.&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Github Repo&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/ChukwurahVictor/altschool_exam" rel="noopener noreferrer"&gt;https://github.com/ChukwurahVictor/altschool_exam&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project live link&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://zippy-kheer-15c249.netlify.app/" rel="noopener noreferrer"&gt;https://zippy-kheer-15c249.netlify.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tailwind Docs&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://tailwindcss.com/docs/guides/create-react-app" rel="noopener noreferrer"&gt;https://tailwindcss.com/docs/guides/create-react-app&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>career</category>
    </item>
  </channel>
</rss>
