I am using useNavigate and this error is coming:
Uncaught Error: Invalid hook call. Hooks can only be called inside the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
The code is following:
import React, { useContext, useEffect } from 'react';
import { useNavigate } from "react-router-dom";
function Recipeitem() {
let navigate = useNavigate;
const gotoRecipes = ()=>{
navigate(`/recipe`);
}
return (
<button onClick={()=>{gotoRecipes()}}>Go to Page</button>
);
}
I have tried many solutions but doesn't work. Please help.
I appreciate any help you can provide.
CodePudding user response:
useNavigate is a react hook (function) not a value. You have to call it with ()
.
let navigate = useNavigate();