Why am I getting error “hook cannot be called inside a callback” when setting state in React?

Functions prefixed with use are interpreted by React to be hooks. But here, your useBackgroundColor isn’t actually a hook, it’s just a state setter function. Rename it to something more appropriate – the usual convention is to prefix the state variable with set. Change

const [backgroundColor, useBackgroundColor] = useState("purple");


const [backgroundColor, setBackgroundColor] = useState("purple");

and then reference setBackgroundColor in your code.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top