javascript 通过使用useNavigate()函数,我得到此错误[已关闭]

omvjsjqw  于 2022-12-02  发布在  Java
关注(0)|答案(1)|浏览(113)

Closed. This question is not reproducible or was caused by typos . It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 hours ago.
Improve this question
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.

n6lpvg4x

n6lpvg4x1#

useNavigate是一个react钩子(函数)而不是一个值。你必须用()来调用它。

let navigate = useNavigate();

相关问题