React Tailwind(css) Alert Component Example

WorldGoIT
3 min readAug 16, 2023

--

React Tailwind(css) Alert Component Example

1. What is React tailwindcss Alert component?

React and tailwindcss are popular frameworks and libraries in modern web development. React is a JavaScript library for building user interfaces, and tailwindcss is a CSS framework for styling. You can combine the two to create a React tailwindcss notification component.

Notification components are used to convey important information to users or to encourage interaction. For example, you can display warnings, success messages, notification messages, and more. These notifications help improve the user experience of your web application and provide users with the information they need.

2. Why use React and tailwindcss together?

Using React with tailwindcss provides developers with a number of advantages.

First, React uses a Virtual DOM to improve performance. The virtual DOM only applies changes to the real DOM, eliminating the need to re-render the entire page. This helps to efficiently handle dynamic elements such as notification components.

Second, tailwindcss makes styling easier by providing predefined style utility classes. By combining classes, you can easily apply the style you want, and you can easily implement responsive design. This helps in flexible configuration of the notification component’s appearance and layout.

3. Component example (code)

alert component

Alert Component Example



import React, { useState, useEffect } from ‘react’;

interface IAlertProps {

icon: JSX.Element;

headline: string;

headlineColor: ‘text-green-600’ | ‘bg-red-600’;

content: JSX.Element;

hideClose?: boolean;

bgColor: ‘bg-green-100’ | ‘bg-red-100’;

timer?: number;

}

const Alert = ({ icon, headline, headlineColor, hideClose, timer, bgColor, content }: IAlertProps) => {

const = useState(true);

useEffect(() => {

if (timer) {

const timeId = setTimeout(() => {

// After xx seconds set the show value to false, if exist timer as props

setAlert(false);

}, timer);

return () => {

clearTimeout(timeId);

};

}

}, );

const closeAlert = () => {

setAlert(false);

};

return (

{isAlertOpen && (

{!hideClose && (

)}

{icon}

{headline}

{content}

)}

);

};

export default Alert;

Props explained

icon: It is literally an icon in svg format. In the example screen, it means a check mark icon.

headline & headlineColor: Put the title in string format, and decorate the color with tailwindcss `text…`. (required, can be modified)

content: Content can be entered in any format as long as it is jsx, and must be entered.

hideClose : It can also be disabled with the close button on the right.

bgColor : As the background color of the Alert window, you can also enter it using tailwincss `bg…`.

timer: SetTimeout & clearTimeout means how long the window disappears. In the example, 3 seconds (3000) were entered.

Example of using Alert Component (import)

Codesandbox

reference

--

--

No responses yet