It turned out that, at least for my specific use case, there was no need to use a class or interact with React lifecycle methods. My code was already working with the component as a function but for some reason I wasn’t able to see it in that moment.
I have probably made a mistake while testing.
But I’ve made some research an discovered that the guys at React have an argument against using class in javascript. To address things that could just be done through classes in the past they have created the hooks functionality to be used by functional React components.
If you have the need to interact with the React state, take a look at the new state hook as everything seems to be functional now on at modern React applications. This would be my next step since even the guys from React are advising against using classes on new React components.
So now I will answer my own questions with what I’ve found in my research in order to help some one that really needs this:
Digging for a solution, I’ve found out that the problem is caused by the function useBlockProps. Is there a way to avoid it without loosing the useBlockProps provided functionalities?
At the present moment it seems to be no way to use classes and still have the useBlockProps functionality since it seems not to be implemented for classes by the folks from WordPress and React Hooks doesn’t work with classes.
Am I doing something wrong?
Yes, I had no need to use a class nor to direclty interact with React lifecycle methods since my functional code was already working.
Is it not allowed anymore?
useBlockProps
is a hook so it was probably created targeting functional components only. So, it will not be able to be used with React classes components but React classes components can still be used. To use WordPress React classes now will be a little bit difficult though since the WordPress folks seems not to be supporting it.
Is there a better alternative to be able to use the React lifecycle?
Yes, it seems that it can be done now from a functional component with the useState
hook that can be imported directly from the @wordpress/element
package the source code seems to just be importing it directly from React with no modification.
Do it if you have the need to do it but do take care of the possible colateral effects.