How to get value of selected page template in Gutenberg editor?

I slightly modified SkyShab’s code, because it fired template change event on page load and it didn’t fire when template was changed to default (since getEditedPostAttribute( ‘template’ ) is then ”, which equals to null when testing for template change)

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

    constructor() {
        this.template = null;
    }

    init() {

        subscribe( () => {

            const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
            if (newTemplate !== undefined && this.template === null) {
                this.template = newTemplate;
            }

            if ( newTemplate !== undefined && newTemplate !== this.template ) {
                this.template = newTemplate;
                this.changeTemplate();
            }

        });
    }

    changeTemplate() {
        // do your stuff here
        console.log('template changed to ${this.template}');
    }
}

new PageTemplateSwitcher().init();