Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 836
Nafeo Joy Avatar asked Jul 12 '20 12:07

Nafeo Joy


People also ask

How do I fix attempted import error useHistory is not exported from react router Dom?

To Solve Attempted import error: 'useHistory' is not exported from 'react-router-dom' Error If You are using react-router-dom v6 then useHistory() is replaced by useNavigate() so You Just need to use useNavigate(). Replace history. push with navigate. Replace history.

Why useHistory is not found in react router Dom?

To solve the error "export 'useHistory' (imported as 'useHistory') was not found in 'react-router-dom'", use the useNavigate hook instead, e.g. const navigate = useNavigate() . The hook returns a function that lets you navigate programmatically. Copied!

Is not exported from react?

The React. js "Attempted import error 'X' is not exported from" occurs when we try to import a named import that is not present in the specified file. To solve the error, make sure the module has a named export and you aren't mixing up named and default exports and imports.


1 Answers

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

You can use:

import { useNavigate } from 'react-router-dom'; const navigate = useNavigate(); navigate('/home'); 
like image 137
Mohamed MILADI Avatar answered Sep 24 '22 15:09

Mohamed MILADI