history icon indicating copy to clipboard operation
history copied to clipboard

createHashHistory v4 and withRouter all actions are POP

Open gitclonedcush opened this issue 6 years ago • 2 comments

createHashHistory v4, withRouter, history.push(), all actions are POP

Hi,

I've noticed that when using withRouter to get history from props and a hashRouter with createHashHistory, all route changes result in a POP action. This happens even when calling history.push

I need to be able to distinguish between PUSH and POP events.

Steps to reproduce

// customHistory.js
import { createHashHistory } from 'history';

const hashHistory = createHashHistory();

hashHistory.listen((location, action) => {
    console.log(action)
});

export default hashHistory;
import { history } from './customHistory';

<Router history={history}>
import { withRouter } from 'react-router-dom';

class CustomButton extends React.Component {
    handleClick = () => {
        this.props.history.push('/route');
    };
    ...
}

export default withRouter(CustomButton);

What's happening?

Example callstack history.push() -> ... -> handleHashChange -> handlePop. handlePop sets the action to POP.

Current Workaround

Instead of using withRouter I have to import customHistory.js and use the history directly, instead of using from props.

gitclonedcush avatar Aug 21 '19 15:08 gitclonedcush

"react-router-dom": "^5.1.2",
I encountered the same problem

webxiaobian avatar Dec 17 '19 03:12 webxiaobian

facing same issues with history.push.

History is being pushed, but pop state is being fired and somehow page is not routing to next state of history. (Checked manually with browser forward navigation click)

vaibhavarora14 avatar Dec 22 '20 06:12 vaibhavarora14