Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: React Toast Notifications In SPFX
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.
1 |
md spfx-ToastNotication |
Navigate to the above-created directory.
1 |
cd spfx-ToastNotication |
Run the Yeoman SharePoint Generator to create the solution.
1 |
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,
1 |
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)
To intialize the toast notification,
1 2 3 4 5 |
const { addToast } = useToasts(); addToast(mycontent, { appearance: 'success', autoDismiss: false, } |
Available apearance paramters are success, error, warning and info in default autodismiss is false.
You can set autodismiss by providing milliseconds.
1 2 3 4 |
<ToastProvider autoDismiss autoDismissTimeout={6000} 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 2 3 4 5 6 7 8 9 10 11 12 |
import * as React from 'react'; import { ISpfxToastProps } from './ISpfxToastProps'; import { MyReactToast }from './Reacttoast'; export default class SpfxToast extends React.Component<ISpfxToastProps, {}> { public render(): React.ReactElement<ISpfxToastProps> { return ( <div> <MyReactToast mycontent={"Hello it's Peter"}/> </div> ); } } |
in Reacttoast.tsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import * as React from 'react'; import { ToastProvider,useToasts } from 'react-toast-notifications'; const ToastDemo = ({mycontent}) =>{ const { addToast } = useToasts(); return ( <> <button onClick={() => addToast(mycontent, { appearance: 'success', autoDismiss: false, PlacementType :'bottom-left', })}> success Toast </button> <button onClick={() => addToast(mycontent, { appearance: 'error', autoDismiss: false, })}> error Toast </button> <button onClick={() => addToast(mycontent, { appearance: 'warning', autoDismiss: false, })}> warning Toast </button> <button onClick={() => addToast(mycontent, { appearance: 'info', autoDismiss: false, })}> Info Toast </button></> ); }; export const MyReactToast = ({mycontent}) => ( <ToastProvider> <ToastDemo mycontent={mycontent} /> </ToastProvider> ); |
Print article | This entry was posted by Peter on February 20, 2020 at 8:19 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |