gpt4 book ai didi

reactjs - 尝试导入错误 : 'useHistory' is not exported from 'react-router-dom'

转载 作者:行者123 更新时间:2023-12-03 20:25:16 30 4
gpt4 key购买 nike

useHistory 给出此错误:

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


react-router-dom 版本:

4.3.1


代码:
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

最佳答案

在 react-router-dom V6 useHistory() 被 useNavigate() 取代
您可以使用:

import {useNavigate} from 'react-router-dom';
const navigate = useNavigate();
navigate('/home')

关于reactjs - 尝试导入错误 : 'useHistory' is not exported from 'react-router-dom' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62861269/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com