【发布时间】:2021-02-03 07:20:56
【问题描述】:
我已经实现了 useHistory(),就像我在下面实现的一样,以前我没有收到任何错误,但是在这个组件中我收到了一个错误
第 6:18 行:在函数“showPost”中调用 React Hook “useHistory”,该函数既不是 React 函数组件也不是自定义 React Hook 函数 react-hooks/rules-of-hooks
import React from 'react';
import CloseIcon from '@material-ui/icons/Close';
import {useHistory} from 'react-router-dom';
const showPost = () =>{
let history = useHistory();
const closeUpload = () =>{
history.goBack();
}
return(
<div className="overlay">
<div className="overlay-container">
<button className="close-btn" onClick = {closeUpload}><CloseIcon color="action"/></button>
<h1>Hello</h1>
</div>
</div>
);
}
export default showPost;
请帮我解决这个问题。 提前谢谢你。
【问题讨论】:
-
美好的一天。你使用的是什么版本的 React 和 React Router?您需要 React 16.8+ 和 React Router 5.1 到 6.0(useHistory() 在 5.1 之前或 6.0 之后不起作用)。
标签: javascript reactjs react-hooks react-router-dom