-
Notifications
You must be signed in to change notification settings - Fork 961
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
history.block unmount and remounts the component back #589
Comments
I can confirm this behavior. In addition, the URL changes before the user makes any decision in the The same behavior occurs when I use the |
Please include sandboxes (https://codesandbox.io) with running demos. @plan8studios The |
What if the following code from In Facebook if I have the unfinished input, back behavior is like what @plan8studios said. If the code will not removed, should we add some props to control the pop event like the following code to control the popstate behavior: createBrowserHistory({
// ...other config
revertPopState: true, // false performs like Facebook, no history.go(delta)
}) And for local usage, the config may changed in some use cases, so add the method: const history = createBrowserHistory();
history.config({
revertPopState: false,
}); OR history.block("Block string", { revertPopState: false }); |
Experiencing exact same issue. Is there a fix for this? Obviously this is caused because there is no way to prevent the initial pop state even if you instantly revert the pop state. When used with react router it will automatically reload the route. There needs to be some sort of middleware that prevents the default event from happening and filters all the pop state events. |
I've created a workaround for this. It is hackish, but it works fantastic. The fix shims the The shim introduces two new methods;
Here is the gist with the shimmed Instead of importing import createBrowserHistory from './createBrowserHistory';
const history = createBrowserHistory(); Here is a sample component with the new blocking/unblocking usage. import React from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
class BlockingComponent extends React.Component {
static propTypes = {
children: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
PropTypes.string,
]),
history: PropTypes.object,
promptBeforeTransition: PropTypes.func,
};
static defaultProps = {
className: null,
};
componentDidMount() {
const { history, promptBeforeTransition } = this.props;
if (promptBeforeTransition && !history.locked) {
history.lock(promptBeforeTransition);
}
}
componentWillUnmount() {
const { history, promptBeforeTransition } = this.props;
if (promptBeforeTransition) {
history.unlock();
}
}
render() {
const { children } = this.props;
return <div>{children}</div>;
}
}
export default withRouter(BlockingComponent); Implementation import React from 'react';
import
class OtherComponent extends React.Component {
render() {
return (
<BlockingComponent
promptBeforeTransition={(navigateToNext) => {
if (someCondition) {
new Promise().resolve(() => navigateToNext()); // Asynchronous usage
}
// If we returned true, transition would occur without calling navigateToNext();
// Returning false stops the transition from happening
return false;
}}
>
<div>...</div>
</BlockingComponent>
);
}
} |
I've written custom blocking condition using history.block, it's triggering but on confirm "cancel" it still push on history, and remounts the current current component back.
Basically, I've timer running in the component using redux, and on dialog cancel it re-mounts the component and timer restarts again.
React Router 4.2.0
React Router DOM 4.1.1
The text was updated successfully, but these errors were encountered: