Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strange behavior of history.goBack() #388

Closed
Pong420 opened this issue Jan 6, 2020 · 5 comments
Closed

strange behavior of history.goBack() #388

Pong420 opened this issue Jan 6, 2020 · 5 comments

Comments

@Pong420
Copy link

Pong420 commented Jan 6, 2020

Minimal CodeSandbox Demo

Step to reproduce

  1. Open the demo
  2. Navigate to /about
  3. Click on Go Back browser history back button
    Screen Shot 2020-02-07 at 8 39 00 PM

Expected console.log

Home 1
About 2
Home 3

Actual console.log

Home 1
About 2
About 3
Home 3
@AlexReff
Copy link

AlexReff commented Feb 6, 2020

You forgot to include event.preventDefault() before your history call. You provided an href value, so the browser tries to navigate to it, then the click handler performs a history action, causing the duplication.

Before:

<a href="#" onClick={() => history.goBack()}>

After:

<a href="#" onClick={(e) => e.preventDefault(), history.goBack()}>

@Pong420
Copy link
Author

Pong420 commented Feb 7, 2020

Thanks your reminder.
But the issues still occur if click on the browser history back button

@agriffis
Copy link

@Pong420 I think what you're seeing is that effects run immediately when props change, but components (and routes) wait to render until the next tick (request animation frame).

You can see this if you add some logging for the rendering:

function Home() {
  useHooks("Home");
  console.log("rendering Home");
  return (
    <div>
      <h2>Home</h2>
    </div>
  );
}

function About() {
  useHooks("About");
  console.log("rendering About");
  return (
    <div>
      <h2>About</h2>
    </div>
  );
}

Now your sequence produces:

rendering Home 
Home 1 -- effect ran at component mount
rendering About 
About 2 -- effect ran at component mount
rendering About 
About 3 -- effect ran when "back clicked"
rendering Home 
Home 3 -- effect ran at component mount

I'm not sure why we don't also see an extra effect when links are clicked as we see from browser back and forward, but in any case, it's something to be aware of: Effects run when props they depend on change, even when the component is preparing to unmount.

@Pong420
Copy link
Author

Pong420 commented Feb 21, 2020

Effects run when props they depend on change, even when the component is preparing to unmount.

you are right, thanks

@Pong420 Pong420 closed this as completed Feb 21, 2020
@halil-akgun
Copy link

halil-akgun commented Aug 6, 2023

You forgot to include event.preventDefault() before your history call. You provided an href value, so the browser tries to navigate to it, then the click handler performs a history action, causing the duplication.

Before:

<a href="#" onClick={() => history.goBack()}>

After:

<a href="#" onClick={(e) => e.preventDefault(), history.goBack()}>

Thank you very much.
I was struggling for an hour. Thanks to you, I solved the problem by doing the following:

onClick={(e) => { e.preventDefault(); props.history.goBack(); }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants