React toast notifications tutorial

React toast notifications tutorial

In this post we will give you information about React toast notifications tutorial. Hear we will give you detail about React toast notifications tutorialAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to add toast notificationsto our react app using the react-toastify library.

Installation

Let’s install the react-toastify library to our react app.

Run the below command in your terminal.

npm i react-toastify

Adding toast notifications

Example

App.js
import React from "react";import { ToastContainer, toast } from "react-toastify";import "react-toastify/dist/ReactToastify.css";function App() {  <span>function Notify() {    toast("You clicked the button");  }  return (    &lt;div className="App"&gt;      &lt;ToastContainer /&gt;      &lt;button onClick={Notify}&gt;Run toast&lt;/button&gt;    &lt;/div&gt;  );}export default App;

In the above code, we first imported a toast method, <ToastContainer> component from the react-toastify and also ‘ReactToastify.css’ file for the default styling.

  • toast( ) : The toast method takes the content as a first argument, it means what content we need to see during the toast notification (like we added You clicked the button in the above code).

  • ToastContainer: To render the toast we need to add a ToastContainer component in our app.

Changing toasts position

By default toasts will be shown on top-right position of your browser, we can change the toast position by adding a second argument to the toast method.

  function Notify() {    toast("You clicked the button",{        <span>//toast is positioned on bottom right        position: "bottom-right"    });  }

These are the other available position values.

top-right,top-center,top-leftbottom-right,bottom-center,bottom-left

Closing toasts or disable

By default, toasts will be auto closed after 5000 milliseconds (5 seconds), we can delay the toasts closing time by adding an autoClose property to the toast method.

  function Notify() {    toast("You clicked the button",{        position: "bottom-right",        //toast will be closed after 10 seconds        autoClose: 10000    });  }

We can also disable the auto close by setting autoClose property to false.

  function Notify() {    toast("You clicked the button",{        position: "bottom-right",        autoClose: false    });  }

You can find more information about react-toastify library in GitHub.

Hope this code and post will helped you for implement React toast notifications tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US