Open In App

React Suite Notification With Toaster

React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application.

In this article, we’ll learn about React Suite Notification with Toaster. The notification component is used to display a notification message globally. The notification is also used with a toaster in react-based applications.



React Suite Notification With toaster Props:

Toaster Methods:



Syntax: 

<Notification type={'info'} 
    header={'Info'} closable>
    ....
</Notification>

<Button onClick={() => toaster.push('Message', 
    { placement: 'bottomCenter' })}>
    ...
</Button>

Creating React Application And Installing Module:

Step 1: Create a React application using the given command:

npm create-react-app projectname

Step 2: After creating your project, move to it using the given command:

cd projectname

Step 3: Now Install the rsuite node package using the given command:

npm install rsuite

Project Structure: Now your project structure should look like the following:

 

Example 1: Below example demonstrates the basic notification with the toaster component.




import { Button, Notification, useToaster } from "rsuite";
import "rsuite/dist/rsuite.min.css";
  
export default function App() {
  
    const toaster = useToaster();
  
    const message = (
        <Notification type={'success'
                      header={'Success'
                      closable>
            <h4>
                You have successfully send the message.
            </h4>
        </Notification>
    );
  
    return (
        <center>
            <div>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite Notification with toaster
                </h4>
  
                <div style={{ marginTop: 20, width: 400 }}>
                    <Button onClick={() => 
                        toaster.push(
                            message, 
                            { placement: 'topCenter' }
                        )}>
                            Send
                    </Button>
                </div>
            </div>
        </center>
    );
}

Step to Run Application: Run the application using the following command from the project’s root directory.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

 

Example 2: Below example demonstrates the notification with a custom duration toaster.




import { Button, Notification, useToaster } from "rsuite";
import "rsuite/dist/rsuite.min.css";
  
export default function App() {
  
    const toaster = useToaster();
  
    const message = (
        <Notification type={'info'
                      header={'Info'
                      closable 
                      duration={5000}>
            <h4>
                Your message has been delayed by 5 sec.
            </h4>
        </Notification>
    );
  
    return (
        <center>
            <div>
                <h2>GeeksforGeeks</h2>
                <h4 style={{ color: "green" }}>
                    React Suite Notification with toaster
                </h4>
  
                <div style={{ marginTop: 20, width: 400 }}>
                    <Button onClick={() => 
                        toaster.push(
                            message, 
                            { placement: 'bottomCenter' }
                        )}>
                            Send
                    </Button>
                </div>
            </div>
        </center>
    );
}

Output:

 

Reference: https://rsuitejs.com/components/notification/#with-toaster


Article Tags :