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

Directly called window.history.pushState is ignored by RR 3.2.1 #6304

Closed
kirill-konshin opened this issue Aug 24, 2018 · 4 comments
Closed

Comments

@kirill-konshin
Copy link
Contributor

kirill-konshin commented Aug 24, 2018

I am using quite an outdated version of RR and I am facing an issue when directly called windown.history.pushState is ignored by RR 3.2.1.

Version

"react-router": "3.2.1"

Test Case

https://codesandbox.io/s/84w232m6r9

Steps to reproduce

Simply click "Go to About" which will call window.history.pushState(null, null, '/about').

Expected Behavior

Router transitions to appropriate page.

Actual Behavior

Nothings happens.

@kirill-konshin kirill-konshin changed the title Directly called history.pushState is ignored by RR 3.2.1 Directly called window.history.pushState is ignored by RR 3.2.1 Aug 24, 2018
@pshrmn
Copy link
Contributor

pshrmn commented Aug 24, 2018

You will need to navigate using your application's history object, not the global window.history object.

history uses the observer pattern; callbacks are registered with a history instance and whenever navigation happens, the callbacks are called.

This only works for navigation performed by the history object (e.g. history.push(), history.replace() and history.go()).

const history = createBrowserHistory();
history.listen(() => {
  console.log("ooh, a navigation!");
});
history.push("/abou");

// console
> ooh, a navigation!

Navigation outside of a history object, cannot be detected by a history object, so it cannot know to inform its observers (the callbacks) about the navigation.

@pshrmn pshrmn closed this as completed Aug 24, 2018
@kirill-konshin
Copy link
Contributor Author

Thanks for the quick answer! But that's completely not what I was looking for.

Unfortunately, I cannot use RR's history, because window.history.pushState is being called outside of the React app (we embed React app in a main app), so I was expecting that RR will respect browser's default functionality.

Any other suggestions? Please reopen...

@pshrmn
Copy link
Contributor

pshrmn commented Aug 24, 2018

Your app's history instance is the one source of truth for navigation. There is no way for React Router to detect history.pushState() calls because there is no event emitted from them (as opposed to the popstate event that is emitted for window.history.go() calls). Any navigation done outside of your history instance (even with a second `history instance) is essentially invisible to React Router.

As for possible solutions, you can create a history module that is importable throughout your application so that you can navigate from outside of React.

// history.js
import { createBrowserHistory } from "history";

// actual implementation might be slightly more involved
// see: https://github.com/ReactTraining/react-router/blob/v3/modules/browserHistory.js
export default createBrowserHistory();
// react-app.js
import history from "./history";
ReactDOM.render(
  <Router history={history} />,
  root
);
// main-app.js
import history from "./history";

history.push({ pathname: "/about" });

@kirill-konshin
Copy link
Contributor Author

Yep, looks like sharing the history object is the only possible solution...

@lock lock bot locked as resolved and limited conversation to collaborators Oct 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants