Search code examples
reactjstypescriptswitch-statementversionreact-router-dom

useHistory is not exported in version 6.0.0-beta.0?


"react-router": "^6.0.0-beta.0",
"react-router-dom": "^6.0.0-beta.0",

The Material UI 'Devias Material Kit Pro' template has this version and useHistory doesn't work, is there any other property with a different name that works the same? I used navigate.push (history.push) with useNavigate and it doesn't work.

useHistory only works for version 5.2.0?

useHistory error useHistoryerror


Solution

  • As of react-router@6.x this functionality has moved to the useNavigate hook. So the replacement for history.push/history.replace may now look like:

    const { useNavigate } from 'react-router-dom'
    
    ...
        const navigate = useNavigate()
        ...
        navigate({ pathname: './some/path' }) // instead of navigate.push
        // or
        navigate({ pathname: './some/path' }, { replace: true }) // navigate.replace