A simple and reusable React toast (alert) component with context β customizable, lightweight, and easy to use in any React project.
- β Custom alert messages like toasts
- β Easy setup using React Context
- β Works across the whole project
- β Auto dismiss after timeout
- β Customizable look and duration
npm install react-custom-toast-simple-
- Wrap Your App in ToastProvider
// App.jsx or root layout
import { ToastProvider } from 'react-custom-toast-simple';
function App() {
return (
<ToastProvider>
<YourApp />
</ToastProvider>
);
}-
- Place the Toast Component Once (e.g., in Layout)
import { BToast } from 'react-custom-toast-simple';
const Layout = () => {
return (
<>
<Navbar />
<BToast /> {/* Toast pop-up component */}
<Outlet />
<Footer />
</>
);
};
-
- Use the customAlert() Function Anywhere
import { useCustomAlert } from 'react-custom-toast-simple';
const MyComponent = () => {
const customAlert = useCustomAlert();
const handleClick = () => {
customAlert('This is a custom toast!', 2000); // optional second arg = duration (ms)
};
return <button onClick={handleClick}>Show Toast</button>;
};You can customize the look of the toast by editing the toast component styles:
<div className="bg-red-900 text-white w-72 p-4 text-center rounded-lg">
<p>{toastText}</p>
</div>
customAlert("Saved successfully!", 1500);- First parameter: Message to display
- Second parameter: Duration in milliseconds (optional, default: 1500ms)
- MIT β Free to use, modify, and share.
Ashraf Nishat
GitHub Profile