Using HOC withRouter with React Router 4

I didn’t know that when a component is not being rendered by your router, it still can use the router properties history, match and location when you wrap it in the withRouter HOC:

import { withRouter } from 'react-router-dom'

...

this.props.history.push('/some-route')

...

export default withRouter(ThisComponent)

See: Programmatically navigate with React Router.

Leave a comment

Your email address will not be published. Required fields are marked *