React Toast Notifications In SPFX

Introduction

 
The react-toast-notifications is a plugin written in React.js to generate toast notifications in React doms. Since this plugin is written in a React hooks concept, we are not able to use this in class component. Rather, we can use this in functional component.
 
Open a command prompt. Create a directory for the SPFx solution.
 
md spfx-ToastNotication
 
Navigate to the above-created directory.
 
cd spfx-ToastNotication
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have the default name (spfx-ToastNotication in this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
 
Permissions to access web APIs
 
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
Selected choice - SpfxToast
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
 
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,
 
npm shrinkwrap
 
In the command prompt, type the below command to open the solution in the code editor of your choice.
 
NPM Packages Used,
 
On the command prompt, run the below commands
  1. npm i react-toast-notifications  
Necessary imports for Toast Notification plugin,
  1. import { ToastProvider, useToasts } from 'react-toast-notifications' ;
Wrap your ToastComponent in the ToastProvider, which provides context for the Toast descendants.
e.g) 
  1. export  const  MyReactToast = ({mycontent}) => (  
  2.     <ToastProvider>  
  3.       <ToastDemo mycontent={mycontent} />  
  4.     </ToastProvider>  
  5.   );  
To intialize the toast notification,
  1. const { addToast } = useToasts();  
  2. addToast(mycontent, {  
  3.      appearance: 'success',  
  4.      autoDismiss: false,  
  5.    }  
Available apearance paramters are success, error, warning and info in default autodismiss is false.
 
You can set autodismiss by providing milliseconds.
  1. <ToastProvider  
  2.         autoDismiss  
  3.         autoDismissTimeout={6000}  
  4.         placement="bottom-center">  
Available placements are 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right' 
 
The useToasts hook contains following signatures,
  1. const { addToast, removeToast, removeAllToasts, updateToast, toastStack } = useToasts();  
 in SpfxToast.tsx
  1. import * as React from 'react';  
  2. import { ISpfxToastProps } from './ISpfxToastProps';  
  3. import { MyReactToast }from './Reacttoast';  
  4. export default class SpfxToast extends React.Component<ISpfxToastProps, {}> {  
  5.   public render(): React.ReactElement<ISpfxToastProps> {  
  6.     return (  
  7.       <div>  
  8.       <MyReactToast mycontent={"Hello it's Madhan"}/>  
  9.         </div>  
  10.     );  
  11.   }  
  12. }  
in Reacttoast.tsx
  1. import * as React from 'react';  
  2. import { ToastProvider,useToasts } from 'react-toast-notifications';  
  3.   
  4. const  ToastDemo  = ({mycontent}) =>{  
  5.   const { addToast } = useToasts();  
  6.   return (  
  7.       <>  
  8.     <button onClick={() => addToast(mycontent, {  
  9.       appearance: 'success',  
  10.       autoDismiss: false,  
  11.       PlacementType :'bottom-left',  
  12.     })}>  
  13.       success Toast  
  14.     </button>  
  15.     <button onClick={() => addToast(mycontent, {  
  16.       appearance: 'error',  
  17.       autoDismiss: false,  
  18.     })}>  
  19.       error Toast  
  20.     </button>  
  21.     <button onClick={() => addToast(mycontent, {  
  22.       appearance: 'warning',  
  23.       autoDismiss: false,  
  24.     })}>  
  25.       warning Toast  
  26.     </button>  
  27.     <button onClick={() => addToast(mycontent, {  
  28.       appearance: 'info',  
  29.       autoDismiss: false,  
  30.     })}>  
  31.       Info Toast  
  32.     </button></>   
  33.       
  34.   );  
  35. };  
  36. export  const  MyReactToast = ({mycontent}) => (  
  37.     <ToastProvider>  
  38.       <ToastDemo mycontent={mycontent} />  
  39.     </ToastProvider>  
  40.   );  
Output
 
 
For more about reat toast notification visit here.
 

Conclusion

 
Hence we learned about initializing toast notifications in Spfx form using the react-toast-notifications plugin. I hope this helps someone. Happy coding :)