question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to persist current history state when replacing state with navigate method

See original GitHub issue

Hello there General Kyoetic 🖖

Today, we ran into an issue with a self contained component that uses the History api to manage past states. That component pushes a new state with a state object when any of its values change (form type component) and also listens to the popstate event to then extract the state object and rehydrate the previous values in its React state. This is done to avoid unnecessary round trips to the API when we had already loaded these values previously.

All was fine with this component’s life cycle when browsing through history with the Back and Forward buttons, however that stopped working when another unrelated component on the page started using raviger’s navigate method with the replace boolean set to true.

The reason is that the navigate method always pushes a null state object (first parameter of history.replaceState). https://github.com/kyeotic/raviger/blob/74e3b87eaf108d0950ab09f368f1f91f23345dcb/src/navigate.js#L25 This causes our component to have lost the state object it had pushed, and thus it fails to rehydrate values properly.

We currently fixed it by replacing the navigate(url, true) call by this:

// reuse the current history state when replacing state
window.history.replaceState(history.state, null, url);
dispatchEvent(new PopStateEvent('popstate', null));

But ideally we’d like to have a way to do replace state with raviger’s navigate while still reusing the current history.state.

Do you think there would be a way to introduce either an option, or a state object parameter to the navigate method?

Thanks a lot for your help 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
yenoiwesacommented, Mar 12, 2021

I personally think that allowing to pass your own state object is more versatile than the option to persist the current state. Also makes it closer to the native API so easy for users to understand. That is my opinion 😃

0reactions
yenoiwesacommented, Apr 7, 2021

FYI I just integrated the change and it looks to be working fine! Thanks again 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with history, pushState and replaceState in Javascript
Another way to change your browser's history state is to use the replaceState function. It works almost exactly as the pushState method ......
Read more >
Preserving Form State in Refreshes and Navigation with React
Using the History API to store meaningful state that preserves across refreshes and page navigations.
Read more >
5 Methods to Persisting State Between Page Reloads in React
One of the straightforward options is to use localStorage in the browser to persist the state. Let's take a look at an example....
Read more >
How to update the router state on history.replace in react router?
However when I am using history.replace in javascript I am not able to update the state. I am trying the below code however...
Read more >
History.pushState() - Web APIs | MDN
The state object is a JavaScript object which is associated with the new history entry created by pushState() . Whenever the user navigates ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found