Attempted import error: 'useHistory' is not exported from 'react-router-dom'
Asked Answered
S

13

355

useHistory giving this error:

Failed to compile ./src/pages/UserForm/_UserForm.js Attempted import error: 'useHistory' is not exported from 'react-router-dom'. This error occurred during the build time and cannot be dismissed.

react-router-dom version:

4.3.1

Code:

import React, { useState, Fragment } from 'react';
import FormUserDetails from './FormUserDetails';
import FormPersonalDetails from './FormPersonalDetails';
import Confirm from './Confirm';
import Success from './Success';
import Button from '@material-ui/core/Button';
import { Grid, makeStyles } from '@material-ui/core';
import { useHistory } from 'react-router-dom';


function UserForm() {
    const [step, setStep] = useState(1);
    const history = useHistory();


    const StepButtons = (props) => (
        <React.Fragment>
            <Grid item xs={4}>
                {props.value !== 'initial' ?
                    <Button variant="outlined" className={classes.button} onClick={(e) => previousStep(e)}>Back</Button> :
                    <Button variant="outlined" className={classes.button} onClick={(e) => reGenerate(e)}>Re-generate</Button>
                }
            </Grid>
            <Grid item xs={4} />
            <Grid item xs={4}>
                {
                    props.value === 'confirm' ?
                        <Button variant="outlined" className={classes.button} style={{ float: "right" }} onClick={(e) => confirm(e)}>Confirm</Button> :
                        props.value !== 'final' ?
                            <Button variant="outlined" className={classes.button} style={{ float: "right" }} onClick={(e) => nextStep(e)}>Continue</Button> :
                            null
                }
            </Grid>
        </React.Fragment>

    );
    const nextStep = (e) => {
        e.preventDefault();
        setStep(prevState => prevState + 1)
    }
    const previousStep = (e) => {
        e.preventDefault();
        setStep(prevState => prevState - 1)
    }
    const reGenerate = (e) => {
        e.preventDefault();
    }
    const confirm = (e) => {
        history.push('/')
    }
    return (
         <div>
            <h1>Hello</h1>
         </div>
    )
}
export default UserForm
Shroudlaid answered 12/7, 2020 at 12:47 Comment(2)
What about React version? Please note: You need to be using React >= 16.8 in order to use useHistory hook!Hospital
React version is already 16.8.4. Thanks for pointing out the 'react-router-dom' version. @HospitalShroudlaid
K
756

In react-router-dom v6 useHistory() is replaced by useNavigate().

You can use:

import { useNavigate } from 'react-router-dom';
const navigate = useNavigate();
navigate('/home');
Kumquat answered 6/4, 2021 at 15:37 Comment(6)
Here you are hardcoding the page to be opened. How to get history here so that I can navigate the user to the previous page ?Spook
I spent more than enough time digging around on react-routers Github profile and couldn't find anything noting the replacement of 'useHistory'...maybe it's there after all, but it wasn't obvious. Thanks for making this obvious!Saleratus
@Spook Agreed, it's not that obvious. See my answer below: https://mcmap.net/q/41047/-attempted-import-error-39-usehistory-39-is-not-exported-from-39-react-router-dom-39Greggs
@Saleratus Agreed, it's not that obvious. See my answer below: https://mcmap.net/q/41047/-attempted-import-error-39-usehistory-39-is-not-exported-from-39-react-router-dom-39Greggs
They absolutely did not need to change that. Now my employer has to pay me to spend time to play the catchup game with react-router team, as if they are my competitor. Component maintainers should not make life more difficult for the people using their components. Standard engineering courtesy.Ann
reactrouter.com/docs/en/v6/hooks/use-navigate The navigate function has two signatures: Either pass a To value (same type as <Link to>) with an optional second { replace, state } arg or Pass the delta you want to go in the history stack. For example, navigate(-1) is equivalent to hitting the back button.Pathology
A
130

Replace useHistory with useNavigate then

const navigate = useNavigate();

and then replace history.push('/path') with navigate('/path')

Change history.replace('/path') with navigate('/path', { replace: true })

Want to use state in push/navigate do navigate('/path', { state: { name:'Xyz' }})

Attain answered 30/12, 2021 at 12:51 Comment(1)
Do you mean useNavigate(), because useNavigation() gives not an exported member error.Gullible
G
46

None of the answers actually mention how to replicate the methods which used to be available from the v5 useHistory hook but which are no longer available in v6 e.g. goBack, goForward, go. The following example is taken from the migration guide:

React Router v5 app using methods from useHistory hook:

// This is a React Router v5 app
import { useHistory } from "react-router-dom";

function App() {
  const { go, goBack, goForward } = useHistory();

  return (
    <>
      <button onClick={() => go(-2)}>
        Go 2 pages back
      </button>
      <button onClick={goBack}>Go back</button>
      <button onClick={goForward}>Go forward</button>
      <button onClick={() => go(2)}>
        Go 2 pages forward
      </button>
    </>
  );
}

Here is the equivalent app with v6 using navigate method from useNavigate hook:

// This is a React Router v6 app
import { useNavigate } from "react-router-dom";

function App() {
  const navigate = useNavigate();

  return (
    <>
      <button onClick={() => navigate(-2)}>
        Go 2 pages back
      </button>
      <button onClick={() => navigate(-1)}>Go back</button>
      <button onClick={() => navigate(1)}>
        Go forward
      </button>
      <button onClick={() => navigate(2)}>
        Go 2 pages forward
      </button>
    </>
  );
}
Greggs answered 8/3, 2022 at 11:57 Comment(2)
> Attempted import error: 'useHistory' is not exported from 'react-router-dom'. useHistory is no more exported member, V6 uses useNavigate()Flanna
@M.AbdullahIqbal Thanks for your message, yes, my answer already says to use useNavigate() for v6.Greggs
P
32

useHistory is replace by useNavigate in v6.

Just replace useHistory name to useNavigate, or follow these 3 simple steps.

  1. import { useNavigate } from 'react-router-dom' ; on the top of your file.

  2. const navigate = useNavigate(); define in your function.

  3. navigate("/path_name"); redirect to your path.

Pshaw answered 22/4, 2022 at 11:48 Comment(0)
S
16

I upgraded the version of react-router-dom to

5.2.0

and it is working now.

Shroudlaid answered 12/7, 2020 at 12:55 Comment(1)
Right, useHistory was launched in 5.2.0Keever
C
10

V6 has scrapped useHistory hook. now useNavigate hook is used.
Usage

import {useNavigate} from 'react-router-dom';

let say you have a function name sendData

function sendData(){
    let data = {name,email,password}
    let results = await fetch('http://localhost:8000/api/register',{
        method:'POST',
        headers:{
            'Content-Type': 'application/json',
            'Accept':'application/json'
        },
        body:JSON.stringify(data)
    });
    results = await results.json();
    console.warn("results",results);
    localStorage.setItem('user',JSON.stringify(results))
    navigate('../add',{replace:true})
}

To Go Back use navigate('../add',{replace:true})
To Go Forward use navigate('/add')

Charmian answered 31/12, 2022 at 8:37 Comment(0)
K
9

Just replace useHistory with useNavigate this way:

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

Then

const navigate = useNavigate();

Last,

navigate("/home");
Kidron answered 10/10, 2022 at 11:25 Comment(0)
K
8

Based on above answers am adding an additional points if the issue exists with v5

If you stick to react-router-dom v5, You have to upgrade to 5.2.0 and the issue will be resolved, because it was introduced in the 5.2.0


Or you can upgrade to v6 and use the useNavigate instead of useHistory

import { useNavigate } from 'react-router-dom';

const navigate = useNavigate();

navigate('/home');
Keever answered 1/3, 2023 at 18:30 Comment(0)
C
7
import { useNavigate } from 'react-router-dom';

const navigate = useNavigate();

const confirm = (e) => {
    navigate.push('/')
}
Carbonous answered 10/1, 2022 at 2:59 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Kemberlykemble
looks like this doesn't work anymore eg. "is not a function" - on v6Dialyze
G
5

In react-router-dom v6 useHistory() function is replaced by useNavigate().

const navigate = useNavigate();

navigate("/login")

Gunpaper answered 21/5, 2022 at 9:44 Comment(0)
B
4

If you must stick to the latest react-router-dom v6.0.0, then replace useHistory with useNavigate. Else, downgrade your react-router-dom to v5.2.0 and your code works as it should.

Bandwagon answered 5/11, 2021 at 15:24 Comment(0)
S
4

the newer version has useNavigate, which u can import from 'react-router-dom'

import { useNavigate } from 'react-router-dom';
Sulfonate answered 20/5, 2023 at 22:50 Comment(1)
Your answer could be improved with the help of supporting informationGhee
H
2

react-router-dom v5 useHistory, but in v6 you should use useNavigate hook

Usage:

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

function SignupForm() {
  let navigate = useNavigate();

  async function handleSubmit(event) {
    event.preventDefault();
    await submitForm(event.target);
    navigate("../success", { replace: true });
  }

  return <form onSubmit={handleSubmit}>{/* ... */}</form>;
Heterophony answered 11/4, 2022 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.