Create Custom Alerts in Your React Application With Chakra UI
Alerts are messages displayed on a website/web application to convey important information to a user. They play a crucial role in web applications. There are many ways to create alerts in React; Chakra UI makes the process easy and efficient.
Chakra UI is a popular component library for React that provides a set of customizable and accessible UI components.

Installing Chakra UI
To use the Chakra UI library,one of many react component libraries, you first need to install it. You can install it by running the following terminal command in your node.js project’s directory:
Alternatively, you can install Chakra UI using Yarn. To do so, run the following command:

Setting Up Chakra UI
After installing Chakra UI, you must make it available in your application. To do this, you need to set up theChakraProvidercomponent.
TheChakraProvidercomponent is a top-level component that the Chakra UI library provides. It wraps the entire application and provides the theme and styling context to all its components.

To set up theChakraProvidercomponent, import it from@chakra-ui/react:
TheChakraProvidercomponent supports athemeprop. Passing thethemeprop to theChakraProvidercomponent ensures that all Chakra UI components in the application can access the provided theme and styling context. Thethemeprop is optional; if you don’t pass it, Chakra UI will use a default theme.

Creating a Custom Alert Using Alert Components
Chakra UI offers four components that let you create a custom alert:Alert,AlertIcon,AlertTitle, andAlertDescription.
To create your alert message, import these components from the Chakra UI library and use them as follows:

After importing the components, theAlertcomponent displays a message to the user. It has astatusprop set to “success,” which indicates that the message is a success message.
There are three other statuses: “info,” “error,” and “warning.” The color scheme and icon that the alert uses will depend on the message status.
TheAlertcomponent contains three children:AlertIcon,AlertTitle, andAlertDescription. TheAlertIconcomponent displays a small icon next to the message,AlertTitledisplays the main message, andAlertDescriptiondisplays a more detailed description of the message.
The previous code block will generate an alert that looks like this:
Customizing Alert Messages Using the Variant Prop
To customize the appearance of the alert message, use thevariantprop of theAlertcomponent. Thevariantprop defines the visual appearance of the alert message and determines the color scheme, icon, and font weight of the message based on the value you pass to it.
Thevariantprop accepts several string values such assubtle,solid,left-accent,top-accent,right-accent, andbottom-accent. Each value represents a different visual style of the alert message.
Here is an example of four alert components with different variants:
Rendering the code block above will display a custom alert like this:
Customizing Your Alert Messages Using the className Prop
Rather than sticking with the default appearance of the alert messages, you can customize it using theclassNameprop. You use theclassNameprop to define a CSS class and apply your custom style to the alert message.
For example:
In this example, the alert component has a CSS class “alert”. After defining the CSS class, you can define your styles in your CSS file.
The code above will apply the CSS styles to the alert component. If you are familiar with Chakra UI style props, you should use them to style alert messages instead of theclassNameprop.
After applying the CSS styles above, the alert component will display as seen in the image below:
Triggering Alert Messages in Response to User Events
You have created an alert component that consistently displays an alert message on the screen. However, to enhance user experience, you can trigger the alert message in response to specific events a user initiates,using JavaScript event Listeners.These events could include clicking a button, submitting a form, or encountering an error.
To trigger your alert message in response to events, use React state and thedisplayprop of the Chakra UI components.
This code block manages the state of the notification display with theuseStatehook. It sets the initial state of the notification display to “none,” which hides the notification.
When the user clicks theButton, it calls thenotifyfunction. Calling the notify function changes the value of thedisplaystate from “none” to “flex.”, making the notification visible.
When the user clicks thecloseButton, it calls the close function. It changes the state of the display back to “none” which hides the notification.
Now You Can Create Customizable Alerts
Now you have learned how to create a custom alert in your React application using Chakra UI. With Chakra UI, creating custom alerts in React is easy and intuitive, allowing us to provide clear and concise information to our users. Chakra UI provides many other customizable and accessible UI components to help you build great React applications.
Follow this quick tutorial and learn about the benefits of component-based styling.
The key is not to spook your friends with over-the-top shenanigans.
Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.
These plugins will make you wonder why you used Photoshop in the first place.
You’ve been quoting these famous films wrong all along!
Taming data is easier than it looks.