DevExtreme React - Show and Hide the Toast

API

NOTE
In this article, the Button widget is used to demonstrate how to show and hide the Toast. This choice is made for purely demonstrational purposes, and you can do the same operations using another widget following the same guidelines.

To show or hide the Toast programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Toast, respectively.

JavaScript
$(function() {
    $("#toastContainer").dxToast({
        message: "Connection problem",
        type: "error"
    });

    $("#buttonContainer").dxButton({
        text: "Show the Toast", 
        onClick: function () {
            $("#toastContainer").dxToast("show");
            // ---------- or ----------
            $("#toastContainer").dxToast("toggle", true);
        } 
    });
});

With Angular, AngularJS, or Knockout, use a different technique. Bind the visible property of the Toast widget to a component property (in Angular), a scope property (in AngularJS), or an observable variable (in Knockout). After that, change this property or variable, and the Toast will appear or disappear.

Angular
HTML
TypeScript
<dx-toast
    [(visible)]="isVisible"
    type="error"
    message="Connection problem">
</dx-toast>
<dx-button
    text="Show the Toast"
    (onClick)="isVisible = true">
</dx-button>
import { DxToastModule, DxButtonModule } from "devextreme-angular";
// ...
export class AppComponent {
    isVisible: boolean = false;
}
@NgModule({
     imports: [
         DxButtonModule,
         DxToastModule,
         // ...
     ],
     // ...
 })
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-toast="{
        message: 'Connection problem',
        type: 'error',
        bindingOptions: {
            visible: 'isToastVisible'
        }
    }"></div>
    <div dx-button="{
        text: 'Show the Toast',
        onClick: showToast
    }"></div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.isToastVisible = false;
        $scope.showToast = function () {
            $scope.isToastVisible = true;
        }
    });
Knockout
HTML
JavaScript
<div data-bind="dxToast: {
    message: 'Connection problem',
    type: 'error',
    visible: isToastVisible
}"></div>
<div data-bind="dxButton: {
    text: 'Show the Toast',
    onClick: function (e) {
        e.model.isToastVisible(true);
    }
}"></div>
var viewModel = {
    isToastVisible: ko.observable(false)
};

ko.applyBindings(viewModel);

User Interaction

Because the Toast is supposed to notify a user when something happens, it cannot be invoked from the UI. However, it may be hidden from the UI in many different ways. To decide which of them are available to the user, specify the following options.

Option Description
closeOnClick Hides the Toast when a user clicks/presses it.
closeOnOutsideClick Hides the Toast when a user clicks/presses outside of it.
closeOnSwipe Hides the Toast when a user swipes it out of the screen.
closeOnBackButton Hides the Toast when a user presses the Back button on the device.

Events

To execute certain commands before or after the Toast was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the widget, assign it to the corresponding onEventName option. For example, in the following code, a handler of the hidden event is assigned to the onHidden option. This handler counts down from three replacing the message in the Toast at the same time.

jQuery
JavaScript
$(function() {
    var counter = 3;

    $("#toastContainer").dxToast({
        message: counter,
        displayTime: 500,
        onHidden: function (e) {
            counter--;
            if (counter != 0) {
                e.component.option("message", counter);
            } else {
                e.component.option({
                    message: "Go!",
                    onHidden: undefined,
                    displayTime: 3000,
                    type: "success"
                });
            }
            e.component.show();
        }
    });

    $("#buttonContainer").dxButton({
        text: "Start the Countdown", 
        onClick: function () {
            $("#toastContainer").dxToast("show");
        } 
    });
});
Angular
HTML
TypeScript
<dx-toast
    [(visible)]="isVisible"
    [displayTime]="displayTime"
    [message]="message"
    (onHidden)="onHidden($event)"
    [type]="type">
</dx-toast>
<dx-button
    text="Start the Countdown"
    (onClick)="isVisible = true">
</dx-button>
import { DxToastModule, DxButtonModule } from "devextreme-angular";
// ...
export class AppComponent {
    isVisible: boolean = false;
    counter: number = 3;
    displayTime: number = 500;
    type = "info";
    message: string = this.counter.toString();
    onHidden (e) {
        this.counter--;
        if (counter != 0) {
            this.message = this.counter.toString();
        } else {
            this.message = "Go!";
            this.onHidden = undefined;
            this.displayTime = 3000;
            this.type = "success";
        }
        this.isVisible = true;
    }
}
@NgModule({
     imports: [
         DxButtonModule,
         DxToastModule,
         // ...
     ],
     // ...
 })

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
var hiddenEventHandler1 = function (e) {
    // First handler of the "hidden" event
};

var hiddenEventHandler2 = function (e) {
    // Second handler of the "hidden" event
};

$("#toastContainer").dxToast("instance")
    .on("hidden", hiddenEventHandler1)
    .on("hidden", hiddenEventHandler2);
See Also