Nader's Daily Blog
Welcome to Every Developers favorite blog in the Devosphere

New product features | The latest in technology | The weekly debugging nightmares & more!

Creating notifications never been so easy

Creating notifications never been so easy

July 24, 2023

Nader Elmahdy

Nader Elmahdy

Take my knowledge and shove it up your brain...

It only takes 3 lines of code to create a beautiful notification in React js

UI

When dealing with data mutations like updating users' data, it would be cool if the user gets notified that his changes were successful.

react-toastify team has done a great job allowing us to create beautiful notifications with only 3 lines of code.


Installation

To get started simply run npm i react-toastify

Usage

Toast.tsx

1import "react-toastify/dist/ReactToastify.css";
2import { ToastContainer, toast } from "react-toastify";
3export default function Toast() {
4  const notify = () => toast("Your address has been updated");
5  return (
6    <div>
7          <ToastContainer autoClose={3000} />
8      <button onClick={()=>{notify()}}>Notify Me!</button>
9    </div>
10  )
11}

The above code will simply create a button when clicked will trigger a browser notification.

This is just a simple example, in a real scenario, you'd call the function notify after a certain action is taken like updating data or smth...


More details can be found on npm

You might also like: