【发布时间】:2022-01-25 04:30:22
【问题描述】:
我试图在我的 React 函数之一中使用 useParams() 钩子,但它给出了以下错误
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a
function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
我试图调用 useParams() 的 React 组件看起来像这样
const IndividualEvent = () => {
const [showError, setShowError] = useState(false);
const [event, setEvent] = useState(null);
const [showLoader, setShowLoader] = useState(true);
const Fetchevent = () => {
const {eventName} = useParams();
alert(eventName);
}
useEffect(() => {
Fetchevent();
})
return (
<div id="individual-event">
{
showLoader ? <Loader text='Fetching event details'/> : null
}
<Sidebar />
<div id="event-container" className="page">
</div>
</div>
)
}
这里的错误是什么以及如何解决它
【问题讨论】:
-
这能回答你的问题吗? Error: Invalid hook call how can i solve it?
-
另外,rules of hooks.
标签: reactjs react-hooks react-router frontend