32

How to use useHistory() correctly? I can't make the transition from one react component to another. According to the instructions from the React documentation and also here on Stack Overflow, I cannot make the transition from App.js to MyComponent.js.

For example - I am trying

/* **App.js ** */
/* Import modules */
import React from 'react';
import { useHistory } from 'react-router-dom'; // version 5.2.0

function App()
{
    let history = useHistory ();
    const handleClick = () => {
       history.push ('./pages/MyComponent');
    }    

    return (
       <div className="App">
          <button onClick={handleClick}>Next page ==></button>
       </div>
    );
}

I also tested this example, but the output throws the following error when the button is pressed:

TypeError: Cannot read property 'push' of undefined

Does something seem to be leaking to me or is there a mistake on Babel's side?

Project react structure:

+ Root/
    + src/
        - App.js
        - index.js
        + pages/
            - MyComponent.js
    
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23

6 Answers6

93

This has changed in v6, useHistory is now useNavigate and we can use it as follows:

instead of:

const history = useHistory()
history.push('/')

we now use:

const navigate = useNavigate()
navigate('/')
Newbyte
  • 2,421
  • 5
  • 22
  • 45
HibaHasan
  • 1,255
  • 2
  • 5
  • 12
24

You can't just use the useHistory hook to redirect to another page. You need to properly set up your application in order to use React Router. Look at their examples starting from this https://reactrouter.com/web/example/basic

You need to wrap your entire application with <BrowserRouter /> which will give the history object you are looking for through the hook.

By the way, you don't give a relative file path to history.push as an argument, you must give a valid route that you typically setup using <Route /> component

maxpaj
  • 6,029
  • 5
  • 35
  • 56
Kyubbbbbb
  • 346
  • 1
  • 4
4

Using React 17.0>, this works for me:

import { useHistory } from "react-router-dom";

const history = useHistory();
history.push("/home");

I've reached too much to find this correctly use of the useHistory function. Try and answer this post for feedback.

Luca
  • 1,588
  • 2
  • 22
  • 26
Kün
  • 49
  • 1
1

you need to use it with react-router-dom. set your router config and then push it to that path. you can get more information by looking at documentation.

https://reactrouter.com/web/example/route-config

do not forget to set your switch components and your exact for root path.

kubilay
  • 21
  • 1
  • 2
1

Using history.replace('/<route-name>') also works.

Fruno
  • 81
  • 10
  • I'm downvoting this because the problem is that he doesn't have `history` in scope in the first place, not that the push method isn't working and was looking for an alternative method. In his case, neither push nor replace would work. – coblr Mar 14 '23 at 00:42
1

When you are applying history.push, is that your path name? history.push('/pathname') is the process I guess. You can see here: https://reactrouter.com/web/api/Hooks

SGhosh
  • 41
  • 4