Ant Design Library has this component pre-built, and it is very easy to integrate as well. In response to user operations, the Message Component is used for displaying global messages as feedback. We can use the following approach in ReactJS to use the Ant Design Message Component.
Message Props:
- content: It is used to denote the content of the message.
- duration: It is used to denote the Time in seconds before auto-dismiss, don’t dismiss if set to 0.
- onClose: It is a callback function that is triggered when the message is closed.
Message Config Props:
- className: It is used for the customized CSS class.
- content: It is used to denote the content of the message.
- duration: It is used to denote the Time in seconds before auto-dismiss, don’t dismiss if set to 0.
- icon: It is used for the customized icon.
- key: It is used for the unique identification of the Message.
- style: It is used for the customized inline style.
- onClick: It is a callback function that is triggered when the message is clicked.
- onClose: It is a callback function that is triggered when the message is closed.
Message Config Default Props:
- duration: It is used to denote the Time in seconds before auto-dismiss.
- getContainer: It is used to return the mount node for Message.
- maxCount: It is used to denote the Max message show.
- prefixCls: It is used for the prefix className of message node.
- rtl: It is used to indicate whether to enable RTL mode or not.
- top: It is used to denote the distance from the top.
Global static method:
- message.config(options): This method is used for the global configuration of messages. It takes the options object as a parameter whose props are explained above (Refer to Message Config Default Props).
- message.destroy(): This method is used to remove a message. This method does not take any parameters.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install antd
Project Structure: It will look like the following.
Example: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React from 'react' import "antd/dist/antd.css" ; import { Button, message } from 'antd' ; export default function App() { return ( <div style={{ display: 'block' , width: 700, padding: 30 }}> <h4>ReactJS Ant-Design Message Component</h4> <> <Button type= "primary" onClick={() => { message.info( 'Message Content!' ); }} > Click me to see Message </Button> </> </div> ); } |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://ant.design/components/message/