You need to have the method historyState() as part of the methods of your class component. If you want to save something, just make that method return the data you’ll later need. Every time the route gets accessed or removed from the history, it will immediately execute with whatever you passed along on the history’s state.
The same method gets called on two situations, so check for the arguments to know which one it is. If they are undefined, you’re navigating away from the route. If they are not undefined, then that means you had stored something previously on that route before leaving it earlier.
Also keep in mind that historyState is called before the initial lifecycle methods.
Thanks for your reply,
I tried using this method to navigate between one page and another, with the param saving ‘from’ to indicate where the state came from, but I noticed that if I was to navigate between them multiple times or one page twice it would sometimes seem to be setting the wrong state or the state from the previous page.
I also noted that when using setHistoryState(), with the hash passed as the second parameter, I couldn’t seem to get a result on the page I intended when using
historyState(params: any) {
if(params) {
console.log('the passed, set params are: ', params) // no params
}
...
Is there anything in the config I could have set that would break this functionality? I have made sure that clearHistory is not set to true on the route.