<?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: Olabisi Olaoye</title>
    <description>The latest articles on DEV Community by Olabisi Olaoye (@olabisi09).</description>
    <link>https://dev.to/olabisi09</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%2F1050122%2F8f0c2b6d-55fe-4958-87a3-ad46a2abdbd5.JPG</url>
      <title>DEV Community: Olabisi Olaoye</title>
      <link>https://dev.to/olabisi09</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olabisi09"/>
    <language>en</language>
    <item>
      <title>How to Pass Data Across Routes with React Router</title>
      <dc:creator>Olabisi Olaoye</dc:creator>
      <pubDate>Sat, 26 Aug 2023 20:05:40 +0000</pubDate>
      <link>https://dev.to/olabisi09/how-to-pass-data-across-routes-with-react-router-53jm</link>
      <guid>https://dev.to/olabisi09/how-to-pass-data-across-routes-with-react-router-53jm</guid>
      <description>&lt;p&gt;URL parameters are a good way of passing data from page to page in a React application. But what if you don't want that data to show in your URL? Here's a great way to achieve that using the &lt;code&gt;useLocation&lt;/code&gt; hook that React Router provides. If you're new to how React Router works, you can check out their &lt;a href="https://reactrouter.com/" rel="noopener noreferrer"&gt;docs&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll be persisting an object in our navigation to another page that looks 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;{
  id: 123,
  name: 'Jane Doe'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Installing dependencies
&lt;/h2&gt;

&lt;p&gt;If you're using npm, run this command to install React Router in your application:&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;If you're using Yarn, do this:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Route Setup
&lt;/h2&gt;

&lt;p&gt;In your &lt;code&gt;App.js&lt;/code&gt; file, you'll have to define each route in your application. For simplicity, we'll be defining two routes, one for each page.&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 Page1 from './Page1';
import Page2 from './Page2';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

export default function App() {
  return (
    &amp;lt;Router&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path={'/'} element={&amp;lt;Page1/&amp;gt;}/&amp;gt;
        &amp;lt;Route path={'/page2'} element={&amp;lt;Page2/&amp;gt;}/&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/Router&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Note that a &lt;code&gt;Route&lt;/code&gt; component must have a parent &lt;code&gt;Routes&lt;/code&gt; component to wrap it. &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Passing Data to Page
&lt;/h2&gt;

&lt;p&gt;In the component for the first page, we define an object to be passed to the next page. We'll be using React Router's &lt;code&gt;Link&lt;/code&gt; component for this, which is pretty similar to rendering an &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag. The &lt;code&gt;Link&lt;/code&gt; component has a &lt;code&gt;state&lt;/code&gt; prop where we can pass in our state without it being encoded in the URL.&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';

export default function Page1() {
  const routeState = {
    id: 123,
    name: 'Jane Doe'
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;React Router Demo&amp;lt;/h1&amp;gt;
      &amp;lt;Link to='/page2' state={routeState}&amp;gt;Next page&amp;lt;/Link&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Retrieving the State in the Next Page
&lt;/h2&gt;

&lt;p&gt;Now that we've passed in our state, we can retrieve it in the destination route by using the &lt;code&gt;useLocation&lt;/code&gt; hook. &lt;code&gt;useLocation&lt;/code&gt; returns an object with information about the current URL, including the &lt;code&gt;state&lt;/code&gt; property. On the destination page, we can simply refer to that state from the 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 from 'react';
import { useLocation } from 'react-router-dom';

export default function Page2() {
  const location = useLocation();

  const id = location.state.id;
  const name = location.state.name;

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Page 2&amp;lt;/h1&amp;gt;
      &amp;lt;div&amp;gt;ID: {id}&amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;Name: {name}&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt;That's it! There are other ways of passing data between pages in React, of course, but this is definitely a good option if you don't want to add any parameters to your URL. You can also view the &lt;a href="https://stackblitz.com/edit/stackblitz-starters-kfndre?file=src%2FPage1.js" rel="noopener noreferrer"&gt;Stackblitz demo&lt;/a&gt; and play around with more routing options.&lt;/p&gt;

&lt;p&gt;Please leave me a comment or reaction to let me know if this post helped. Thank you for reading!&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>File Validation in React with Formik and Yup</title>
      <dc:creator>Olabisi Olaoye</dc:creator>
      <pubDate>Sun, 06 Aug 2023 15:54:24 +0000</pubDate>
      <link>https://dev.to/olabisi09/file-validation-in-react-with-formik-and-yup-48e6</link>
      <guid>https://dev.to/olabisi09/file-validation-in-react-with-formik-and-yup-48e6</guid>
      <description>&lt;p&gt;Dealing with forms in React applications used to be tedious, but with libraries like &lt;a href="https://formik.org/docs/overview" rel="noopener noreferrer"&gt;Formik&lt;/a&gt; and &lt;a href="https://www.npmjs.com/package/yup" rel="noopener noreferrer"&gt;Yup&lt;/a&gt;, handling form state and validating each field is much easier and efficient. Formik does the overall form management and Yup helps to define the structure and rules to be applied to the form.&lt;/p&gt;

&lt;p&gt;When working with file inputs, schema building and validation is not as straightforward as validating a text field, for example. This guide will show how to implement that in a React application.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Installing Dependencies
&lt;/h2&gt;

&lt;p&gt;To install Formik and Yup, run this on your command line:&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 formik yup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using yarn, do this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add formik yup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Handling Form State
&lt;/h2&gt;

&lt;p&gt;The form should consist of a field with an input of type 'file' and a submit button. At this point, the form looks 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;const MyForm = () =&amp;gt; {
  return (
    &amp;lt;form&amp;gt;
      &amp;lt;input type='file' name='myFile' accept='.pdf'/&amp;gt;
      &amp;lt;button type='submit'&amp;gt;Submit Form&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;There are different ways to handle form state with Formik, but in this guide we'll be using the &lt;code&gt;useFormik&lt;/code&gt; hook to directly provide all the Formik helpers needed. Here we can define the initial values of the input field, what happens when the form is submitted, and a validation schema.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useFormik } from 'formik';

const MyForm = () =&amp;gt; {
  const formik = useFormik({
    initialValues: {
      myFile: ''
    },
    onSubmit: () =&amp;gt; {
      console.log('Form submitted!')
    }
  })
  return (
    &amp;lt;form&amp;gt;
      &amp;lt;input type='file' name='myFile' accept='.pdf'/&amp;gt;
      &amp;lt;button type='submit'&amp;gt;Submit Form&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Building the Validation Schema
&lt;/h2&gt;

&lt;p&gt;Next, import Yup into the file to define the set of rules you want the form field to obey. In this guide, we'll be validating the file input in two aspects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The field must be required&lt;/li&gt;
&lt;li&gt;The file must only accept PDF files&lt;/li&gt;
&lt;li&gt;The file size must not be more than 3 megabytes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's what the validation schema looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as Yup from 'yup';

const validationRules = Yup.object().shape({
    myFile: Yup.mixed().required('required')
      .test('fileFormat', 'Only PDF files are allowed', value =&amp;gt; {
        if (value) {
          const supportedFormats = ['pdf'];
          return supportedFormats.includes(value.name.split('.').pop());
        }
        return true;
      })
      .test('fileSize', 'File size must be less than 3MB', value =&amp;gt; {
        if (value) {
          return value.size &amp;lt;= 3145728;
        }
        return true;
      }),
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When validating the file type, we use the &lt;code&gt;test&lt;/code&gt; function to determine whether the file extension is &lt;code&gt;.pdf&lt;/code&gt;. We also have to test if the file size is not more than 3,145,728 bytes (3 megabytes).&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Displaying Errors in Field
&lt;/h2&gt;

&lt;p&gt;We intend to display the validation errors below the field, so we render a paragraph just below the file input:&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;form onSubmit={formik.handleSubmit}&amp;gt;
      &amp;lt;input type='file' name='myFile' accept='.pdf' onChange={handleChange}/&amp;gt;

      &amp;lt;div&amp;gt;{(formik.errors.myFile) ? &amp;lt;p style={{color: 'red'}}&amp;gt;{formik.errors.myFile}&amp;lt;/p&amp;gt; : null}&amp;lt;/div&amp;gt;
      &amp;lt;br/&amp;gt;
      &amp;lt;button type='submit'&amp;gt;Submit Form&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make sure that Formik is actually in charge of the field's state, a function is passed to the input's &lt;code&gt;onChange&lt;/code&gt; property. The function sets the value of the field to Formik's state by using the helper function &lt;code&gt;setFieldValue&lt;/code&gt;, a function Formik provides to set a field's value to something. It takes two arguments: the name of the field and the value to be set. The &lt;code&gt;onChange&lt;/code&gt; function looks 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;const handleChange = (e) =&amp;gt; {
  formik.setFieldValue('myFile', e.target.files[0]);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result of this is that the file is validated every time its value changes and when the form is submitted. Also, Formik's &lt;code&gt;handleSubmit&lt;/code&gt; helper function is passed into the form element's &lt;code&gt;onSubmit&lt;/code&gt; property. This will trigger Formik's own &lt;code&gt;onSubmit&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;Here's the complete 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 from "react";
import { useFormik } from "formik";
import * as Yup from 'yup';

const MyForm = () =&amp;gt; {
  const validationRules = Yup.object().shape({
    myFile: Yup.mixed().required('required')
      .test('fileFormat', 'Only PDF files are allowed', value =&amp;gt; {
        if (value) {
          const supportedFormats = ['pdf'];
          return supportedFormats.includes(value.name.split('.').pop());
        }
        return true;
      })
      .test('fileSize', 'File size must not be more than 3MB', 
      value =&amp;gt; {
        if (value) {
          return value.size &amp;lt;= 3145728;
        }
        return true;
      }),
  })

  const formik = useFormik({
    initialValues: {
      myFile: ''
    },
    onSubmit: () =&amp;gt; {
      console.log('Submitted')
    },
    validationSchema: validationRules,
  })

  const handleChange = (e) =&amp;gt; {
    formik.setFieldValue('myFile', e.target.files[0]);
  };
  return (
    &amp;lt;form onSubmit={formik.handleSubmit}&amp;gt;
      &amp;lt;input type='file' name='myFile' accept='.pdf' onChange={handleChange}/&amp;gt;

      &amp;lt;div&amp;gt;{(formik.errors.myFile) ? &amp;lt;p style={{color: 'red'}}&amp;gt;{formik.errors.myFile}&amp;lt;/p&amp;gt; : null}&amp;lt;/div&amp;gt;
      &amp;lt;br/&amp;gt;
      &amp;lt;button type='submit'&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/form&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;That's it! You can check out the code output on Stackblitz &lt;a href="https://stackblitz.com/edit/react-formik-yup-example-uhdg-2qq4v1?file=MyForm.js" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Thank you for reading, and please let me know of your thoughts about this article.&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to Build a Global Notification System in React</title>
      <dc:creator>Olabisi Olaoye</dc:creator>
      <pubDate>Sun, 30 Jul 2023 17:57:51 +0000</pubDate>
      <link>https://dev.to/olabisi09/how-to-build-a-global-notification-system-in-react-4a9n</link>
      <guid>https://dev.to/olabisi09/how-to-build-a-global-notification-system-in-react-4a9n</guid>
      <description>&lt;p&gt;Notifications are an integral part of a well structured React web application, especially when the user expects some form of feedback from completing a task or making a request. There are many ways of achieving this, but this article will be centered on building a simple notification component with &lt;a href="https://mui.com" rel="noopener noreferrer"&gt;Material UI&lt;/a&gt;. Material UI is a component library for React applications that makes frontend development easier, and it's a good option if you don’t want to build design components from scratch.&lt;/p&gt;

&lt;p&gt;The goal of this guide is to be able to call a function to display the notification like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;showNotification&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Hey&lt;/span&gt; &lt;span class="nx"&gt;there&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tutorial will be adopting TypeScript, so this component is suited for TypeScript projects. You can learn more about TypeScript &lt;a href="https://www.typescriptlang.org/docs" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Installing the dependencies
&lt;/h2&gt;

&lt;p&gt;First, install Material UI in your React application. If you're installing with npm, do this on your command line:&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 @mui/material @emotion/react @emotion/styled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re using Yarn, then do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add @mui/material @emotion/react @emotion/styled
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Types and Interfaces
&lt;/h2&gt;

&lt;p&gt;Create a file in the &lt;code&gt;src&lt;/code&gt; folder of your project. We can call it &lt;code&gt;notificationDemo.tsx&lt;/code&gt;. And with the function call of the notification component, we know that we want to pass in an object as an argument. This object should contain the type of the notification (e.g success, error, info) and the message to be displayed. A more effective way to define the notification types would be to store each one in a type like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//add as many message types as you want&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MessageType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;warning&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create an interface, &lt;code&gt;NotifyProps&lt;/code&gt; to specify how that object will look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;NotifyProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MessageType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

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

&lt;p&gt;Now all that's left is creating the component itself. It will render the Material UI &lt;a href="https://mui.com/material-ui/react-snackbar/" rel="noopener noreferrer"&gt;Snackbar&lt;/a&gt; component. Here's the final result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;showNotification&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;NotifyProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Snackbar&lt;/span&gt; 
          &lt;span class="nx"&gt;anchorOrigin&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;vertical&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;top&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;right&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
          &lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; 
          &lt;span class="nx"&gt;sx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="na"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fixed&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;zIndex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;}}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Alert&lt;/span&gt; &lt;span class="nx"&gt;severity&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;sx&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;100%&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Alert&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Snackbar&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;

&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="nx"&gt;container&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;showNotification&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the &lt;code&gt;ReactDOM.render&lt;/code&gt; function renders the component onto the page. The &lt;code&gt;setTimeout&lt;/code&gt; function determines how long it stays on the page before it disappears.&lt;/p&gt;

&lt;p&gt;That’s it! You can call the &lt;code&gt;showNotification&lt;/code&gt; function anywhere in your application and it will render on the page.&lt;/p&gt;

&lt;p&gt;Please leave me a comment or reaction to let me know if this post helped. Thank you for reading!&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>frontend</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Using React Query with TypeScript</title>
      <dc:creator>Olabisi Olaoye</dc:creator>
      <pubDate>Tue, 11 Jul 2023 20:32:51 +0000</pubDate>
      <link>https://dev.to/olabisi09/using-react-query-with-typescript-16h5</link>
      <guid>https://dev.to/olabisi09/using-react-query-with-typescript-16h5</guid>
      <description>&lt;p&gt;When retrieving data from an API (application programming interface) in React, it's always important to do so in the most suitable way for your application. But because there's more than one way to do it, it proves to be problematic. (Throw in TypeScript and it gets even more complicated.) Many developers have acquainted themselves with React Query, a library that simplifies data fetching and state management.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://tanstack.com/query/v4/docs/react/overview" rel="noopener noreferrer"&gt;React Query documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/" rel="noopener noreferrer"&gt;TypeScript documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using TypeScript with React Query takes the developer experience further by ensuring that the request and response from the API is properly defined. Plus, it saves you from unprecedented runtime errors that JavaScript may not notice. Let's try fetching some data from an online fake API.&lt;/p&gt;

&lt;p&gt;First, we install the React Query package (for data fetching) and the Axios package (for making HTTP requests).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @tanstack/react-query axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Say we want to get all posts from the API. That means we are making a GET request. When we look at the response, it's an array of objects 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;[
  {
    "userId": 1,
    "id": 1,
    "title": "Mary had a little lamb",
    "body": "Mary had a little lamb whose fleece was white as snow."
  },
  ...
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this data, we can create an TypeScript interface to infer what type of data we're looking to receive. It'll look something 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;interface Posts {
  userId: number;
  id: number;
  title: string;
  body: string;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our component, we create an asynchronous function to make the request. Asynchronous functions always return a Promise, but we will take advantage of TypeScript by infering the &lt;code&gt;Posts[]&lt;/code&gt; type on the Promise. This ensures that TypeScript knows what kind of data we're going to get.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getPosts = async (): Promise&amp;lt;Posts[]&amp;gt; =&amp;gt; {
  const posts = await axios.get('https://jsonplaceholder.typicode.com/posts')?.data;
  return posts;
}

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

&lt;/div&gt;



&lt;p&gt;Now we can make proper use of React Query by using the &lt;code&gt;useQuery&lt;/code&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {data, isLoading, isError} = useQuery({
  queryKey: ['get-posts'],
  queryFn: getPosts
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we destructured the hook into an object with the properties above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;data&lt;/code&gt; represents the response from the API call.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isLoading&lt;/code&gt; is a boolean value which is &lt;code&gt;true&lt;/code&gt; when there is no ongoing query attempt.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;isError&lt;/code&gt; is a boolean value which is &lt;code&gt;true&lt;/code&gt; when there was an error fetching the query.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many more properties we can use for different functions, but we'll stick with these three for this example.&lt;/p&gt;

&lt;p&gt;Given the &lt;code&gt;isLoading&lt;/code&gt; and &lt;code&gt;isError&lt;/code&gt; properties, we can decide what to render to the browser at each state of the data fetching process. Here's what the whole code looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function Posts() {
  const getPosts = async (): Promise&amp;lt;Posts[]&amp;gt; =&amp;gt; {
    const posts = await axios.get('https://jsonplaceholder.typicode.com/posts')?.data;
    return posts;
  }
  const {data, isLoading, isError} = useQuery({
    queryKey: ['get-posts'],
    queryFn: getPosts
  })

  if (isLoading) {
    return &amp;lt;div&amp;gt;Loading posts...&amp;lt;/div&amp;gt;
  }

  if (isError) {
    return &amp;lt;div&amp;gt;Oops!&amp;lt;/div&amp;gt;
  }

  return (
    &amp;lt;div&amp;gt;
    {data?.map((post) =&amp;gt; (
      &amp;lt;p key={post.id}&amp;gt;{post.body}&amp;lt;/p&amp;gt;
    ))}
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's important to note that when familiarizing yourself with a library, the documentation is the best place to begin. There you can discover more techniques to solving a problem and how to apply them in different use cases.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
