<?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: Afikode Olusola Emma</title>
    <description>The latest articles on DEV Community by Afikode Olusola Emma (@afikode_emma).</description>
    <link>https://dev.to/afikode_emma</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%2F124198%2F1dba0706-c46c-40cb-9248-042e5192cd05.jpg</url>
      <title>DEV Community: Afikode Olusola Emma</title>
      <link>https://dev.to/afikode_emma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afikode_emma"/>
    <language>en</language>
    <item>
      <title>SETTING UP ENVIRONMENT VARIABLES IN REACT-VITE PROJECTS</title>
      <dc:creator>Afikode Olusola Emma</dc:creator>
      <pubDate>Tue, 28 Feb 2023 11:29:35 +0000</pubDate>
      <link>https://dev.to/afikode_emma/setting-up-environment-variables-in-react-vite-projects-549</link>
      <guid>https://dev.to/afikode_emma/setting-up-environment-variables-in-react-vite-projects-549</guid>
      <description>&lt;p&gt;&lt;strong&gt;INTRODUCTION&lt;/strong&gt;&lt;br&gt;
It is no news that pushing some certain sensitive part of your code to a remote repository is not advisable for security reasons. Exposing things like base url, API keys etc makes your application very vulnerable to attacks. It is therefore imperative that sensitive parts of your codes are saved as environmental variables. This article will take you through keeping some sensitive part of your code when using Vite to set up your reactjs application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SETTING UP ENV VARIABLES&lt;/strong&gt;&lt;br&gt;
To start with, there are quite a number of ways to set up react projects. Using Vite is one of the fastest ways of doing this. Vite helps to set up only necessary files, without adding some unnecessary loads on the setup itself. This article does not focus on Vite, rather it focuses on how to set up environment variables when working with a react project set up with Vite. &lt;/p&gt;

&lt;p&gt;To set up a react project application with Vite, type in  the terminal:&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%2F0fbcghdf66eau1f2gzhy.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%2F0fbcghdf66eau1f2gzhy.png" alt=" " width="628" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This command will give some subsequent actions (these actions are based on technology preference). It is very easy to follow, and you are not likely to have glitches. &lt;/p&gt;

&lt;p&gt;Now, to set up your environment variable, create a file with the name .env (dot env) in your home directory (the same directory with your src folder). In your .env file, create a variable in the like below:&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%2Fyrp2omxhshzl598agq80.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%2Fyrp2omxhshzl598agq80.png" alt=" " width="628" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: Ensure that you start your variable name with VITE_, as only variables with such naming convention will be exposed to the naming convention.&lt;/p&gt;

&lt;p&gt;The next process is how to make use of the variables in the respective files or components where they will be needed. To access the URL and KEY named above, type the following the components where you will be needing them:&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%2Fk8nehq2r6xmty7eoa2co.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%2Fk8nehq2r6xmty7eoa2co.png" alt=" " width="800" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You then have access to these variables and use them for whatever you intend in the respective components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Following this pattern ensures that your env variables do not accidentally leak and get into hands that you do not want. I hope this article has been able to help you have a better understanding of setting up environment variables in vite-react applications.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>LEARN CONTEXT API BY BUILDING A MINI FINANCIAL APP</title>
      <dc:creator>Afikode Olusola Emma</dc:creator>
      <pubDate>Sat, 13 Aug 2022 12:35:00 +0000</pubDate>
      <link>https://dev.to/afikode_emma/learn-context-api-by-building-a-mini-finacial-app-10jj</link>
      <guid>https://dev.to/afikode_emma/learn-context-api-by-building-a-mini-finacial-app-10jj</guid>
      <description>&lt;p&gt;There are different ways to manage data in an application. One of the ways data is managed in a ReactJs application is using of concept of prop drilling. Prop drilling requires that data is passed through every parent component down to the child. This method can pose some form of cumbersome nature and may not be completely scalable. &lt;/p&gt;

&lt;p&gt;In this article, we will be exploring the concept of Context API that makes it easier to manage application state. First we need to understand what context is, how it can be implemented in a React application and then we will build a prototype banking app for proper understanding. To have a proper understanding of this article, some knowledge of React and JavaScript will be required. Let’s first define Context. You should have some basic knowledge of ReactJs before going through this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;Context makes available a way for data to be passed through the component tree without having to pass props through every level downwards. This means that data is managed in a global way. With Context, data can be set globally; while it is accessible directly by any of the children component without the need to pass it through every parent element. To put it simply, Context helps us to avoid nesting through different components level to access data. &lt;br&gt;
Context has some APIs, namely: React.createContext, Context.Cosumer, Context.Provider etc. In this article, we will concentrate on React.createContext and Context.Provider. &lt;/p&gt;

&lt;p&gt;Let’s create our application in the following steps. We will create a react application using create-react-app&lt;/p&gt;

&lt;p&gt;We will use material UI component to style the various interfaces.&lt;br&gt;
Our application is to show how state is managed across the deposit, balance and withdrawal screens of our financial application. &lt;br&gt;
After creating the fakebank app with create-react-app, under the src folder we will create a folder called components, where all our folders including the context component will be stored. In the component folder, create a file context.js. The context folder is where the entire application state is being managed.&lt;/p&gt;

&lt;p&gt;In the context component, some component will be imported from react; namely useState and useContext&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 {useState, useContext}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we call in the createContext method, which provides a way for data to be passed through the component tree without having to manually pass props on every level.&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 {useState, useContext}
import React, { useState, useContext } from "react";

const AppContext = React.createContext();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next thing is to create the component via which the children props will be passed; we will also set the initial states of the balance, withdrawal and deposit screen respectively.&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";

const AppContext = React.createContext();

const AppProvider = ({ children }) =&amp;gt; {
  const initialValues = { balance: 0, deposit: 0, withdrawal: 0 };
  const [inputValue, setInputValue] = useState(initialValues);

  let walletBalance = inputValue.balance;
  let amountDeposit = inputValue.deposit;
  let amountWithdrawn = inputValue.withdrawal;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function above would return the children props wrapped with the AppContext.Provider. The Provider allows consuming components to subscribe to changes in context. Next, we will export our AppContext and AppProvider components.  But first, we will set up a custom hook to make our code cleaner. (NOTE: You may decide not to use a custom hook)&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";

const AppContext = React.createContext();

const AppProvider = ({ children }) =&amp;gt; {
  const initialValues = { balance: 0, deposit: 0, withdrawal: 0 };
  const [inputValue, setInputValue] = useState(initialValues);

  let walletBalance = inputValue.balance;
  let amountDeposit = inputValue.deposit;
  let amountWithdrawn = inputValue.withdrawal;


 return (
    &amp;lt;AppContext.Provider&amp;gt;
      {children}
    &amp;lt;/AppContext.Provider&amp;gt;
  );

}

//custom hook

const useGlobalContext = () =&amp;gt; {
  return useContext(AppContext);
};

export { useGlobalContext, AppProvider };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will wrap the App component in the index.js file with the exported AppProvider.&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;React.StrictMode&amp;gt;
    &amp;lt;AppProvider&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/AppProvider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is to pass the values of the initial state of our application to the AppProvider 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 React, { useState, useContext } from "react";

const AppContext = React.createContext();

const AppProvider = ({ children }) =&amp;gt; {
  const initialValues = { balance: 0, deposit: 0, withdrawal: 0 };
  const [inputValue, setInputValue] = useState(initialValues);

  let walletBalance = inputValue.balance;
  let amountDeposit = inputValue.deposit;
  let amountWithdrawn = inputValue.withdrawal;


 return (
    &amp;lt;AppContext.Provider value={{
        walletBalance,
        amountDeposit,
        amountWithdrawn,
      }}&amp;gt;
      {children}
    &amp;lt;/AppContext.Provider&amp;gt;
  );

}

//custom hook

const useGlobalContext = () =&amp;gt; {
  return useContext(AppContext);
};

export { useGlobalContext, AppProvider };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to create the interfaces of the deposit, withdrawal and balance screens. As said earlier, we will be using the material UI framework to build our application interfaces.&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 {
  Box,
  Card,
  CardContent,
  Typography,
  CardActions,
  Button,
  Divider,
} from "@mui/material";


const Deposit = () =&amp;gt; {
  return (
    &amp;lt;Box
      sx={{
        marginTop: "10rem",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
      }}
    &amp;gt;
      &amp;lt;Card sx={{ minWidth: 700, background: "#A52A2A", color: "white" }}&amp;gt;
        &amp;lt;CardContent&amp;gt;
          &amp;lt;Typography
            sx={{ fontSize: 20, textAlign: "center", fontWeight: "bold" }}
            gutterBottom
          &amp;gt;
            Deposit
          &amp;lt;/Typography&amp;gt;
          &amp;lt;Divider color="white" /&amp;gt;

          &amp;lt;Box
            sx={{ display: "flex", justifyContent: "space-around", mt: "1rem" }}
          &amp;gt;
            &amp;lt;Typography sx={{ mb: 1.5, color: "white" }} color="text.secondary"&amp;gt;
              Balance
            &amp;lt;/Typography&amp;gt;
            &amp;lt;Typography sx={{ mb: 1.5, color: "white" }} color="text.secondary"&amp;gt;
              $100
            &amp;lt;/Typography&amp;gt;
          &amp;lt;/Box&amp;gt;

          &amp;lt;Typography sx={{ fontWeight: "bold" }}&amp;gt;Deposit Amount&amp;lt;/Typography&amp;gt;
          &amp;lt;form&amp;gt;
            &amp;lt;input
              type="text"
              id="deposit"
              name="deposit"
              value="deposit"
            /&amp;gt;
          &amp;lt;/form&amp;gt;
        &amp;lt;/CardContent&amp;gt;
        &amp;lt;CardActions&amp;gt;
          &amp;lt;Button
            variant="contained"
            sx={{ background: "white", color: "black" }}

          &amp;gt;
            Deposit
          &amp;lt;/Button&amp;gt;
        &amp;lt;/CardActions&amp;gt;
      &amp;lt;/Card&amp;gt;
    &amp;lt;/Box&amp;gt;
  );
};

export default Deposit;

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

&lt;/div&gt;



&lt;p&gt;The interface above is for the deposit screen; the same process will be repeated for the other screens. The balance above is hardcoded with a sum of $100, this will be dynamically changes later on.&lt;/p&gt;

&lt;p&gt;After creating the interfaces, in the context component, we need to create event handlers to handle the changes that handle input forms where the amount deposited and withdrawn will be typed; this will be handled in the context 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 React, { useState, useContext } from "react";

const AppContext = React.createContext();

const AppProvider = ({ children }) =&amp;gt; {
  const initialValues = { balance: 0, deposit: 0, withdrawal: 0 };
  const [inputValue, setInputValue] = useState(initialValues);

  let walletBalance = inputValue.balance;
  let amountDeposit = inputValue.deposit;
  let amountWithdrawn = inputValue.withdrawal;


  //handle input change
  const handleChange = (e) =&amp;gt; {
    const { name, value } = e.target;
    setInputValue({ ...inputValue, [name]: value });
  };

  return (
    &amp;lt;AppContext.Provider
      value={{
        walletBalance,
        amountDeposit,
        amountWithdrawn,
        handleChange, 
      }}
    &amp;gt;
      {children}
    &amp;lt;/AppContext.Provider&amp;gt;
  );
};

//custom hook

const useGlobalContext = () =&amp;gt; {
  return useContext(AppContext);
};

export { useGlobalContext, AppProvider };

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

&lt;/div&gt;



&lt;p&gt;The custom hook will be destructured in each of the screens to pass the values in the context component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { walletBalance, amountDeposit, handleChange } =
    useGlobalContext();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below shows the values passed into the deposit component.&lt;/p&gt;

&lt;p&gt;In this article, we have examined how the context API hook can be used to maintain state in a react application, the process of putting the app state in a single components therefore making it easy to pass data across different views.&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 {
  Box,
  Card,
  CardContent,
  Typography,
  CardActions,
  Button,
  Divider,
} from "@mui/material";
import { useGlobalContext } from "./context";

const Deposit = () =&amp;gt; {
  const { walletBalance, amountDeposit, handleChange } =
    useGlobalContext();
  return (
    &amp;lt;Box
      sx={{
        marginTop: "10rem",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
      }}
    &amp;gt;
      &amp;lt;Card sx={{ minWidth: 700, background: "#A52A2A", color: "white" }}&amp;gt;
        &amp;lt;CardContent&amp;gt;
          &amp;lt;Typography
            sx={{ fontSize: 20, textAlign: "center", fontWeight: "bold" }}
            gutterBottom
          &amp;gt;
            Deposit
          &amp;lt;/Typography&amp;gt;
          &amp;lt;Divider color="white" /&amp;gt;

          &amp;lt;Box
            sx={{ display: "flex", justifyContent: "space-around", mt: "1rem" }}
          &amp;gt;
            &amp;lt;Typography sx={{ mb: 1.5, color: "white" }} color="text.secondary"&amp;gt;
              Balance
            &amp;lt;/Typography&amp;gt;
            &amp;lt;Typography sx={{ mb: 1.5, color: "white" }} color="text.secondary"&amp;gt;
              {walletBalance}
            &amp;lt;/Typography&amp;gt;
          &amp;lt;/Box&amp;gt;

          &amp;lt;Typography sx={{ fontWeight: "bold" }}&amp;gt;Deposit Amount&amp;lt;/Typography&amp;gt;
          &amp;lt;form&amp;gt;
            &amp;lt;input
              type="text"
              id="deposit"
              name="deposit"
              value={amountDeposit}
              onChange={handleChange}
            /&amp;gt;
          &amp;lt;/form&amp;gt;
        &amp;lt;/CardContent&amp;gt;
        &amp;lt;CardActions&amp;gt;
          &amp;lt;Button
            variant="contained"
            sx={{ background: "white", color: "black" }}
          &amp;gt;
            Deposit
          &amp;lt;/Button&amp;gt;
        &amp;lt;/CardActions&amp;gt;
      &amp;lt;/Card&amp;gt;
    &amp;lt;/Box&amp;gt;
  );
};

export default Deposit;

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

&lt;/div&gt;



&lt;p&gt;This will be replicated on the other two screens, namely: withdrawal and balance.&lt;/p&gt;

&lt;p&gt;The next thing is to handle the logic that keeps track of the deposit, amount and balance. Remember every logic is handled in the context component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //handle incrementing the deposit made to update balance
  const handleAmount = (e) =&amp;gt; {
    e.preventDefault();

    if (regex.test(amountDeposit)) {
      walletBalance += parseInt(amountDeposit);
      inputValue.balance = walletBalance;
      setInputValue({ ...inputValue, walletBalance });
      amountDeposit = "";
      inputValue.deposit = amountDeposit;
      setInputValue({ ...inputValue, amountDeposit });
    } else {
      alert("You have entered an invalid character!!!");
    }
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The functions updates the balance once deposit is made.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; //handle withdrawals
  const withdrawalHandler = (e) =&amp;gt; {
    e.preventDefault();
    if (regex.test(amountWithdrawn)) {
      walletBalance -= parseInt(amountWithdrawn);
      inputValue.balance = walletBalance;
      setInputValue({ ...inputValue, walletBalance });
      amountWithdrawn = "";
      inputValue.withdrawal = amountWithdrawn;
      setInputValue({ ...inputValue, amountWithdrawn });
    } else {
      alert("You have entered an invalid character!!!");
    }
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pass the handleAmount and withdrawalHandler amount into the provider and get it destructured in their respective components.&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;AppContext.Provider
      value={{
        walletBalance,
        amountDeposit,
        amountWithdrawn,
        handleChange,
        handleAmount,
        withdrawalHandler,
      }}
    &amp;gt;
      {children}
    &amp;lt;/AppContext.Provider&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useGlobalContext on the withdrawal and deposit should look like the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { walletBalance, amountDeposit, handleChange, handleAmount } =
    useGlobalContext();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { walletBalance, amountWithdrawn, handleChange, withdrawalHandler } =
    useGlobalContext();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The balance components is thus:&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 {
  Box,
  Card,
  CardContent,
  Typography,
  Divider,
} from "@mui/material";
import { useGlobalContext } from "./context";

const Balance = () =&amp;gt; {
  const { walletBalance } = useGlobalContext();
  return (
    &amp;lt;Box
      sx={{
        marginTop: "10rem",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
      }}
    &amp;gt;
      &amp;lt;Card sx={{ minWidth: 700, background: "#A52A2A", color: "white" }}&amp;gt;
        &amp;lt;CardContent&amp;gt;
          &amp;lt;Typography
            sx={{ fontSize: 20, textAlign: "center", fontWeight: "bold" }}
            gutterBottom
          &amp;gt;
            Balance
          &amp;lt;/Typography&amp;gt;
          &amp;lt;Divider color="white" /&amp;gt;

          &amp;lt;Box
            sx={{ display: "flex", justifyContent: "space-around", mt: "1rem" }}
          &amp;gt;
            &amp;lt;Typography sx={{ mb: 1.5, color: "white" }} color="text.secondary"&amp;gt;
              Balance
            &amp;lt;/Typography&amp;gt;
            &amp;lt;Typography sx={{ mb: 1.5, color: "white" }} color="text.secondary"&amp;gt;
              {walletBalance}
            &amp;lt;/Typography&amp;gt;
          &amp;lt;/Box&amp;gt;
        &amp;lt;/CardContent&amp;gt;
      &amp;lt;/Card&amp;gt;
    &amp;lt;/Box&amp;gt;
  );
};

export default Balance;

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

&lt;/div&gt;



&lt;p&gt;In this article we have been able to learn the React Context API in a very practical manner by building a financial app that helps keeps track record of deposit, amount and withdrawal. You can dig deeper into more detail by reading the official react documentation &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Basic understanding of React Hooks (useState and useReducer)</title>
      <dc:creator>Afikode Olusola Emma</dc:creator>
      <pubDate>Mon, 10 Jan 2022 17:39:42 +0000</pubDate>
      <link>https://dev.to/afikode_emma/basic-understanding-of-react-hooks-usestate-and-usereducer-1bgl</link>
      <guid>https://dev.to/afikode_emma/basic-understanding-of-react-hooks-usestate-and-usereducer-1bgl</guid>
      <description>&lt;p&gt;What are React Hooks?&lt;br&gt;
React hooks are the new feature introduced in the React version 16.8. They allow us to have state in functional components, and they ensure that we have access to other features of React without writing classes. &lt;br&gt;
When writing functional components, we do not need to write class before adding state; hooks take good care of this. This guide is going to carefully explain to us some common React Hooks, how and when they are used in applications. &lt;br&gt;
In this article, I will take you through a step by step guide into understanding how the useState and useReducer hooks work, and how to implement them in your various projects. Feel free to peruse every line of this writing.&lt;br&gt;
useState Hook&lt;br&gt;
As mentioned earlier, React has to two types of components, namely: Class and Functional components. Class components are ES6 classes that extend React while functional components are simply JavaScript functions. The useState hook allows us to track state in a function component. It is very important to note that a state refers to data or properties that need to be tracked in an application. The react useState hook enables re-render.&lt;br&gt;
Import useState()&lt;br&gt;
To use any of the hooks, they must first be imported from React, and the useState hook is not an exception to this. Below is a code snippet of how to the import useState hook. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpjhhw853ylcrh0hvii6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpjhhw853ylcrh0hvii6.png" alt="Image description" width="800" height="327"&gt;&lt;/a&gt;&lt;br&gt;
The useState hook, like every other hook, after being imported, is destructured.  Below is a code snippet on how useState is used to increase a counter, and the increment is being re-rendered. The code ensures that at each click on the button, the state value is increased by one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foq6d2a5v79mdxici9i10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foq6d2a5v79mdxici9i10.png" alt="Image description" width="800" height="422"&gt;&lt;/a&gt;&lt;br&gt;
The useState hook above contains both the name of the variable ‘counter’ with the initial value set to zero (0), and also the corresponding function ‘setCounter ’ which helps to change the value of the variable. Depending on the problem you intend solving, the initial value of your variable could be any of the data types, namely: string, Boolean, number, object. &lt;br&gt;
To further expand our understanding of the useState hook, I will give an example on how to set the initial value of the variable to a string, and how to display data to user view. The code snippet below shows this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb97iwdebi1urc6w3i2ew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb97iwdebi1urc6w3i2ew.png" alt="Image description" width="800" height="598"&gt;&lt;/a&gt;&lt;br&gt;
The code above sets the initial value of inputValue to a string and with the onChange property of the input, the value typed into the input field is targeted. To display the value to the view, simply place the variable name in curly brackets as seen in the code snippet above.&lt;/p&gt;

&lt;p&gt;useReducer Hook&lt;br&gt;
The useReducer hook is an alternative to the useState hook. It lets you separate the state management from the rendering logic of the component. It is very efficient in managing complex and robust state hooks in React applications. The combination of the useReducer and another hook like the useContext (to be explained in my next article) can be a good and efficient tool to management state.&lt;br&gt;
The useReducer hook accepts a reducer function as its first parameter and an initial state as its second. It returns an array that has both the current state and a dispatch function. The dispatch function allows you to pass as a parameter which can be invoked.&lt;/p&gt;

&lt;p&gt;Import useReducer&lt;br&gt;
To use the useReducer hooks, it must be invoked from react.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3779ryopcthw9v8qr1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3779ryopcthw9v8qr1r.png" alt="Image description" width="800" height="315"&gt;&lt;/a&gt;&lt;br&gt;
The use reducer works in a similar way with Redux. When invoked, it contains a reducer function and the default state of the application. The reducer is executed by the JavaScript reduce() method. The reducer method accepts two parameters, namely, the current state and the action. Like I said earlier, the state is the data that is being manipulated or changed in the application. &lt;br&gt;
The code snippet below shows the reducer and its respective parameters:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn78xb2uewaujx3rypln2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn78xb2uewaujx3rypln2.png" alt="Image description" width="800" height="319"&gt;&lt;/a&gt;&lt;br&gt;
The reducer function accepts an action, this action is executed by the dispatch function. Below is a code snippet of counter app that increases the number of counts each time the button is clicked and it toggled the text at the same time. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvgew02dr8ewtbift17p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvgew02dr8ewtbift17p.png" alt="Image description" width="800" height="1091"&gt;&lt;/a&gt;&lt;br&gt;
From the code above, the initial state is set to zero (0) and the text is shown by setting its value to true. Then a reducer function is created, it accepts the current state of the counter as an argument and an action. The application state get updated by the reducer action type (action.type). &lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
In conclusion, both hooks do the same things, in that they can be used to manage state and re-renders. While useState is a basic hook, useReducer is used for more complex state logic. The useReducer is to be used once the applications gets larger in size, and state transitions are getting complex. &lt;br&gt;
In this article, we went through two react hooks; useState and useReducer. We learned how each one is used and for what reason they will be used. We also saw the advantage of one over the other.&lt;br&gt;
That’s all for now! Feel free to share in the comments section below your thoughts about this article.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using localStorage to store data in the browser</title>
      <dc:creator>Afikode Olusola Emma</dc:creator>
      <pubDate>Fri, 08 Oct 2021 11:35:48 +0000</pubDate>
      <link>https://dev.to/afikode_emma/using-localstorage-to-store-data-in-the-browser-i55</link>
      <guid>https://dev.to/afikode_emma/using-localstorage-to-store-data-in-the-browser-i55</guid>
      <description>&lt;p&gt;It is possible to save data handled by web application without building a database. Two of the properties that allows this to happen are the localStorage and sessionStorage. Both of these are Web Storage APIs mechanisms which basically allow data to be stored in key-value pair. So basically, you have something really similar to the JavaScript object. They do not change after page loads or refresh.  The focus of this article is on localStorage.&lt;/p&gt;

&lt;p&gt;localStorage&lt;br&gt;
Whenever data is saved to the localStorage, it remains intact even after the browser has been closed. This means that data in the localStorage is not affected by the closing and restarting of the browser. To save data in the localStorage, the setItem() method can be used. &lt;br&gt;
Example: localStorage.setItem("name", "value");&lt;/p&gt;

&lt;p&gt;If the value passed is an array, you will have to transform it to a string by using the JSON.stringify() method.&lt;/p&gt;

&lt;p&gt;Example:  localStorage.setItem("name", JSON.stringify(object));&lt;br&gt;
 The name and value represents the key and value of the item being stored.&lt;/p&gt;

&lt;p&gt;Note: The Web APIs key-value pair objects are always in strings, hence the need for the second example above.&lt;br&gt;
Now to retrieve item stored in the database, the getItem() method comes in handy. &lt;br&gt;
Example: localStorage.getItem("name");&lt;br&gt;
 Again, the data retrieved need to be transformed from strings into an object, and we will be using another method called the JSON.parse().&lt;br&gt;
Example: JSON.parse(localStorage.getItem('name'));&lt;/p&gt;

&lt;p&gt;To remove item from the localStorage, the removeItem() method is used. It takes the key of the item to be removed as below:&lt;br&gt;
Example: localStorage.removeItem("name");&lt;/p&gt;

&lt;p&gt;Lastly, you can clear the storage by using the clear() method on the localStorage object.&lt;br&gt;
Example: localStorage.clear();&lt;/p&gt;

&lt;p&gt;Watch out for my next article as I will be writing about sessionStorage() and its respective methods.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
