Toast services in React Toast
04 Sep 202620 minutes to read
The React Toast component provides utility functions for displaying toasts without requiring component instantiation or DOM container setup. This approach enables showing toasts programmatically “on-the-go” with minimal code. The utility functions are ideal for global notifications triggered from anywhere in the application.
Predefined toast types
Display formatted toasts using ToastUtility.show() with predefined types. The Syncfusion React Toast component provides four semantic toast types with built-in styling for common notification scenarios. Specify only the type and content—styling is applied automatically without custom CSS classes.
Available Toast types and their uses:
| Property | Description |
|---|---|
content |
Message text displayed in the React Toast |
type |
Toast type: Information, Success, Error, or Warning (each with predefined styling) |
timeOut |
Display duration in milliseconds; set to 0 for persistent display |
[Class-component]
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
class App extends React.Component {
infoShow() {
toastObj = ToastUtility.show('Please read the comments carefully', 'Information', 20000);
}
successShow() {
toastObj = ToastUtility.show('Your message has been sent successfully', 'Success', 20000);
}
warningShow() {
toastObj = ToastUtility.show('There was a problem with your network connection', 'Warning', 20000);
}
dangerShow() {
toastObj = ToastUtility.show('A problem has been occurred while submitting the data', 'Error', 20000);
}
hideToast() {
toastObj.hide('All');
}
render() {
return (<div>
<div>
<ButtonComponent cssClass="e-btn e-control e-info" onClick={this.infoShow = this.infoShow.bind(this)}> Info Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-success" onClick={this.successShow = this.successShow.bind(this)}> Success Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-warning" onClick={this.warningShow = this.warningShow.bind(this)}> Warning Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-danger" onClick={this.dangerShow = this.dangerShow.bind(this)}> Danger Message </ButtonComponent>
</div>
<br />
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={this.hideToast = this.hideToast.bind(this)}> Hide All </ButtonComponent>
</div>
</div>);
}
}
;
export default App;import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent, ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
class App extends React.Component<{}, {}> {
public infoShow() {
toastObj = ToastUtility.show('Please read the comments carefully', 'Information', 20000);
}
public successShow() {
toastObj = ToastUtility.show('Your message has been sent successfully', 'Success', 20000);
}
public warningShow() {
toastObj = ToastUtility.show('There was a problem with your network connection', 'Warning', 20000);
}
public dangerShow() {
toastObj = ToastUtility.show('A problem has been occurred while submitting the data', 'Error', 20000);
}
public hideToast() {
toastObj.hide('All');
}
public render() {
return (
<div>
<div>
<ButtonComponent cssClass="e-btn e-control e-info" onClick={this.infoShow = this.infoShow.bind(this)}> Info Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-success" onClick={this.successShow = this.successShow.bind(this)}> Success Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-warning" onClick={this.warningShow = this.warningShow.bind(this)}> Warning Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-danger" onClick={this.dangerShow = this.dangerShow.bind(this)}> Danger Message </ButtonComponent>
</div>
<br/>
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={this.hideToast = this.hideToast.bind(this)}> Hide All </ButtonComponent>
</div>
</div>
);
}
};
export default App;[Functional-component]
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
function App() {
function infoShow() {
toastObj = ToastUtility.show('Please read the comments carefully', 'Information', 20000);
}
function successShow() {
toastObj = ToastUtility.show('Your message has been sent successfully', 'Success', 20000);
}
function warningShow() {
toastObj = ToastUtility.show('There was a problem with your network connection', 'Warning', 20000);
}
function dangerShow() {
toastObj = ToastUtility.show('A problem has been occurred while submitting the data', 'Error', 20000);
}
function hideToast() {
toastObj.hide('All');
}
return (<div>
<div>
<ButtonComponent cssClass="e-btn e-control e-info" onClick={infoShow.bind(this)}> Info Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-success" onClick={successShow.bind(this)}> Success Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-warning" onClick={warningShow.bind(this)}> Warning Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-danger" onClick={dangerShow.bind(this)}> Danger Message </ButtonComponent>
</div>
<br />
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={hideToast.bind(this)}> Hide All </ButtonComponent>
</div>
</div>);
}
export default App;import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent, ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
function App(){
function infoShow() {
toastObj = ToastUtility.show('Please read the comments carefully', 'Information', 20000);
}
function successShow() {
toastObj = ToastUtility.show('Your message has been sent successfully', 'Success', 20000);
}
function warningShow() {
toastObj = ToastUtility.show('There was a problem with your network connection', 'Warning', 20000);
}
function dangerShow() {
toastObj = ToastUtility.show('A problem has been occurred while submitting the data', 'Error', 20000);
}
function hideToast() {
toastObj.hide('All');
}
return (
<div>
<div>
<ButtonComponent cssClass="e-btn e-control e-info" onClick={infoShow.bind(this)}> Info Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-success" onClick={successShow.bind(this)}> Success Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-warning" onClick={warningShow.bind(this)}> Warning Message </ButtonComponent>
<ButtonComponent cssClass="e-btn e-control e-danger" onClick={dangerShow.bind(this)}> Danger Message </ButtonComponent>
</div>
<br/>
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={hideToast.bind(this)}> Hide All </ButtonComponent>
</div>
</div>
);
}
export default App;Custom React Toast configuration
For advanced customization beyond predefined types, pass a ToastModel object to the utility function. The ToastModel enables specifying any React Toast property including events, position, close buttons, action buttons, animations, and custom templates. This approach provides full React Toast functionality while maintaining the convenience of utility-based display.
[Class-component]
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
class App extends React.Component {
toastShow() {
toastObj = ToastUtility.show({
title: 'Toast Title',
content: 'Toast shown using utility function with ToastModel',
timeOut: 20000,
position: { X: 'Right', Y: 'Bottom' },
showCloseButton: true,
click: this.toastClick.bind(this),
buttons: [{
model: { content: 'Close' }, click: this.toastClose.bind(this)
}]
});
}
toastClick() {
console.log('Toast click event triggered');
}
toastClose() {
toastObj.hide('All');
}
render() {
return (<div>
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={this.hideToast = this.toastShow.bind(this)}> Show Toast </ButtonComponent>
</div>
</div>);
}
}
;
export default App;import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent, ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
class App extends React.Component<{}, {}> {
public toastShow() {
toastObj = ToastUtility.show({
title: 'Toast Title',
content: 'Toast shown using utility function with ToastModel',
timeOut: 20000,
position: { X: 'Right', Y: 'Bottom' },
showCloseButton: true,
click: this.toastClick.bind(this),
buttons: [{
model: { content: 'Close' }, click: this.toastClose.bind(this)
}]
});
}
public toastClick() {
console.log('Toast click event triggered');
}
public toastClose() {
toastObj.hide('All');
}
public render() {
return (
<div>
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={this.hideToast = this.toastShow.bind(this)}> Show Toast </ButtonComponent>
</div>
</div>
);
}
};
export default App;[Functional-component]
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
function App() {
function toastShow() {
toastObj = ToastUtility.show({
title: 'Toast Title',
content: 'Toast shown using utility function with ToastModel',
timeOut: 20000,
position: { X: 'Right', Y: 'Bottom' },
showCloseButton: true,
click: toastClick.bind(this),
buttons: [{
model: { content: 'Close' }, click: toastClose.bind(this)
}]
});
}
function toastClick() {
console.log('Toast click event triggered');
}
function toastClose() {
toastObj.hide('All');
}
return (<div>
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={toastShow.bind(this)}> Show Toast </ButtonComponent>
</div>
</div>);
}
export default App;import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent, ToastUtility } from '@syncfusion/ej2-react-notifications';
import * as React from "react";
let toastObj;
function App() {
function toastShow() {
toastObj = ToastUtility.show({
title: 'Toast Title',
content: 'Toast shown using utility function with ToastModel',
timeOut: 20000,
position: { X: 'Right', Y: 'Bottom' },
showCloseButton: true,
click: toastClick.bind(this),
buttons: [{
model: { content: 'Close' }, click: toastClose.bind(this)
}]
});
}
function toastClick() {
console.log('Toast click event triggered');
}
function toastClose() {
toastObj.hide('All');
}
return (
<div>
<div className="toastUtility">
<ButtonComponent cssClass="e-btn e-control" onClick={toastShow.bind(this)}> Show Toast </ButtonComponent>
</div>
</div>
);
}
export default App;