How to use historyState()

I’m trying to figure out how on Earth any of this works, the documentation sadly is not very helpful.

What I’m trying to achieve is using history state to set a rail index on my homepage;
This is the code I’m using on my second page:

    const history = Router.getHistory()
    const homeHistoryRecord = history.find((item) => item.hash === 'home')
    if (homeHistoryRecord) {
      Router.replaceHistoryState({ whatis: 'going on' }, homeHistoryRecord.hash)
    }

Then in my homepage I’m trying to use historyState(params: any), or even just log it out with this

 override _enable() {
    console.log('home history state! is', Router.getHistory(), Router.getHistoryState())
  }

But that’s not doing anything.

All I’m trying to achieve is to set the state so that when I arrive on the home page I can see that state and act accordingly.

Hey @dgmip . According to the documentation: LightningJS.io - Documentation

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.

Thank you.