-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using React Hooks #14655
Comments
🤔 Really weird, I can say that I've used hooks in several PRs in the Gutenberg repository without any issue. Can you setup a https://codesandbox.io with a small example? |
@youknowriad I think it should work in a separate gutenberg environment, although I do not really know how to prove it. But the problem I encounter is in the WordPress back office, when rendering the edit function of a custom block. Could it be a conflict of react and react-dom versions between the @wordpress/element module and WordPress core ? |
Yes, probably. Are you trying to bundle @wordpress/element in your wordpress plugin? You shouldn't do that if it's the case as you can only use a single React version at a time. you just rely on the |
Yes that's right, I was actually trying to get the latest React version using the @wordpress/element package, but as you say, I ended up with two different React.
Yes, it looks like I'll have to be patient and wait for WordPress 5.2. thanks a lot for your answer 😃 |
Thanks, closing this issue right now. |
Eventually got the possibility of using hooks by using a copy of React and React DOM, exposed globally. Of course this is super inefficient and hacky -- will make sure to remove this from the plugin I work on once 5.2 gets released. |
I'm trying to use React Hooks (introduced in React 16.8) to build custom blocks. I'm already using React, when I need state management and lifecycle hooks, I use "Component", from the wp.element global object (which is an abstraction of React)
Since this commit : c7d14ca, React Hooks are exposed by wp.element (I'm using the @wordpress/element package to get the last version). But I can't use them.
Here is a simple component :
Here, i'm able to import useState from @wordpress/element package, I don't have any webpack or console error on the import, but I can't use it. The useState(0) call is throwing this error :
Here is my webpack.config.js :
and my .babelrc :
has anyone ever used React Hooks to build custom blocks ?
Some additional informations :
The text was updated successfully, but these errors were encountered: