I know that hooks are the oposite of a class component - don't get me wrong, I just don't know how a simple line of code can make this error appear and what I have to change in that line to make it work in an class component. I got the following example which works very well with .jsx
type of file and with functional components.
This code is just a simple way of share a state between components. So we got the file extendDiv
where we have our state and a simple function to change that state; then we have a global
file, to got that state and distribute it to all of our components: son
(filho) and father
(pai).
In the codesandbox example one
it works just fine, but when I do the exactly same thing but with a class component in the App.jsx
and with some typescript files, it gives me the following error in the following code:
global.isDivExtended = extendDiv();
//Invalid hook call. Hooks can only be called inside of the body of a function component.
You can see all the code and try it here: Codesandbox example two
What I should do here to make it work in an class component? I've already tried to bind
the global and to use the this
... Thank you very much.