Photo by Seriously Low Carb on Unsplash
How to Quickly Add Toast Notifications to Your React App with React Toastify?
React Toastify is a popular library for displaying toast notifications in a React application. Here's a step-by-step guide on how to connect React Toastify to your React application:
Install React Toastify using npm or yarn:
npm install react-toastify
or
yarn add react-toastify
Import the necessary components from React Toastify into your React application:
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
Render the ToastContainer component in your React application:
function App() {
return (
<div>
<ToastContainer />
...
</div>
);
}
Use the toast
function to display a notification:
function handleClick() {
toast('Hello World!');
}
function App() {
return (
<div>
<ToastContainer />
<button onClick={handleClick}>Click Me</button>
</div>
);
}
Customize the notification by passing options to the toast
function:
function handleClick() {
toast.success('Hello World!', {
position: toast.POSITION.TOP_RIGHT
});
}
function App() {
return (
<div>
<ToastContainer />
<button onClick={handleClick}>Click Me</button>
</div>
);
}
You can find more information about React Toastify, including additional customization options, on the official documentation website: fkhadra.github.io/react-toastify.