Home > database >  By using useNavigate() function, I am getting this error
By using useNavigate() function, I am getting this error

Time:12-02

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:

  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
    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();
  • Related