Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react hooks must be called in a react function – how do I change syntax to correct?

this is my first react project and I am trying to create a function based component for a menu bar.
When I try to access useNavigate() I get an error "react hooks must be called in a react function."

how do I change the current code I have to use the line

const navigate = useNavigate();

below is my function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    export const MenuBar = () => (
  <div>
    <Typography variant="h5">Ninja Notes</Typography>
    <Typography variant="h3">{testItem}</Typography>
  </div>
);

then further more, on another page I am trying to use

import { MenuBar } from "./MenuBar";

>Solution :

You should put the useNavigate inside the component that uses it.

If you want to use it inside the MenuBar try

  export const MenuBar = () => {
    const navigate = useNavigate(); // but no need to use the hook if you do not use the navigate variable inside the component.
    return (
      <div>
        <Typography variant="h5">Ninja Notes</Typography>
        <Typography variant="h3">{testItem}</Typography>
      </div>
    );
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading