Accessing this object inside confirm dialog

uiux

#1

Following code is from my Component typescript where I want to route user to a page after user confirmes the same. I get error "cannot read ‘state’ undefined. How to access this object inside confirm area?

import * as dialogs from "ui/dialogs";
dialogs.confirm("Are you sure you want to process?").then(function (proceed) {
            if (proceed) {
                const navigationExtras: NavigationExtras = {
                    queryParams: {
                        states: JSON.stringify(this.state),
                    }
                };
                this.router.navigate(["proceed"], navigationExtras);
            }
        });
  

#2

Hi @rakeshgirase
replace, function (proceed)
with (proceed) =>
and your code will work.

When you use a function object in JS, the this context inside it will change.
If you use arrow function, it won’t change.

so your code will look like this

import * as dialogs from "ui/dialogs";
dialogs.confirm("Are you sure you want to process?").then((proceed) => {
            if (proceed) {
                const navigationExtras: NavigationExtras = {
                    queryParams: {
                        states: JSON.stringify(this.state),
                    }
                };
                this.router.navigate(["proceed"], navigationExtras);
            }
        });

#3

Thanks alot @multishiv19 :slight_smile: