Skip to content
Advertisement

React Hook “useState” is called in function

I have button click system and it works.

JavaScript

Now I want to fetch the API inside the function.

So, change the function clickCreate like this

JavaScript

However there comes error like this.

I should not use useState and useEffect in function, but how can trigger the API by btn click??

JavaScript

Advertisement

Answer

You should move the hook to component level (Rules of hooks), then you can fetch on click and use the hook’s setter:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement