Categories
React Answers

How to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript?

Spread the love

Sometimes, we want to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript.

in this article, we’ll look at how to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript.

How to fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript?

To fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript, we should make sure we call hook functions only on the top level of function components.

For instance, we write

export default () => {
  const classes = useStyles();
  return <AllowanceClass classes={classes} />;
};

to call the useStyles hook at the top of the component.

And then we can use its returned value anywhere in the component.

Conclusion

To fix Invalid hook call. Hooks can only be called inside of the body of a function component with React and JavaScript, we should make sure we call hook functions only on the top level of function components.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *