[v3] SetState on page event


#1

How could I set the state of the page on a specific page event?

E.g. on something like:

$$(document).on('page:init', '.page[data-name="loading"]', function(e) {
  //TODO Set the state somehow
  // The following is not working
  this.setState({
    example: 'Test'
})
});

#2

you shouldn’t use this in callback function or hook, try this:

const self = this;
$$(document).on('page:init', '.page[data-name="loading"]', function(e) {
  self.setState({
    example: 'Test'
  })
});

#3

Thank you for your answer, the problem is, that the on event handler is located in a custom class, that is supposed to be responsible for the template, which unfortunately isn’t the template object itself. Therefore I am looking for a way to get the object, that has that setState method. Propably the template object? But how would I get it?


#4

The Router component is designed to keep component logic in single place, you need to handle this listener inside of component. And if you need to bypass it somewhere else then you need to bypass it external class, etc.