-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
createContext
, useContext
from @wordpress/element
doesn't work on InnerBlocks in custom block
#50633
Comments
createContext
, useContext
from @wordpress/element
doesn't work on InnerBlocks in custom block
Thanks for providing that. It'd be good if it could be installed as a plugin. At the moment it causes some errors. I'm also not really sure I understand the "Step-by-step reproduction steps" that have been provided here. Could you provide some steps that someone could follow to reproduce the issue? Also sharing what the expected and actual outcome is would be good. Contributors are very busy, so the best chance of success is to make it as easy as possible to reproduce the issue. |
@talldan Reproduction is simple: Just create two block with To check this repo: https://github.com/EmranAhmed/gutenberg-context-issue Goto terminal and run
|
I was able to reproduce this. I'm not sure why it happens, but it looks like on the first render If the context provider's value is updated, the child doesn't re-render and so still shows the default context value.
My first thought was that maybe |
I think it's a bug in
Note that there is no common chunk so each generated The solution is to somehow split out the common chunk of context into its own file so that both blocks can share the same context. I don't think it can be easily done in Another idea is to externalize the code for the context, which might be a bit easier. The worst case is to define the context as a global variable, which is often seemed as an anti-pattern in most cases. Not sure if it applies here though. @gziolo Do you have any suggestions for this issue? This looks counter-intuitive and isn't that easy to debug. We might want to do something about this in the build process somehow to prevent future similar confusion 🤔 . |
Thank you for the report @EmranAhmed with the linked repository that helped identify the issue's source.
@kevin940726 was able to precisely identify the issue and offer some ways to address it without making changes to the
@kevin940726, can you explain what would that approach require?
That would be similar to bundling both blocks together. Here, however, it would be the parent block that exposes the context behind the global variable: omport { createContext } from '@wordpress/element'
export const Context = createContext({ id: 0 })
window.MyPluginParent.Context = Context; Then in the child block: const x = useContext(window.MyPluginParent.Context);
It would be great to integrate The Manifest into the webpack config so there is a single file that keeps detailed notes on all the chunks and their dependencies required for a block. This would also allow us to offer some interesting improvements to the developer experience. It would be possible to register multiple blocks from several By the way, not having a way to automatically enqueue the webpack runtime is the reason we still don't have React Fast Refresh working out of the box with multiple entry points (blocks) as explained in #25188 (comment). |
I don't have specific ideas in mind. I think roughly it would look like how we enqueue wp code: make a separate bundle just for the context and make those blocks require them by externalizing the dependency. This should look somehow very similar to the global variable approach though. |
Looking at the provided example, I must say that this behavior is kind of expected, this is how JavaScript bundling works 🙁 The Parent and Child blocks are built separately, as independent scripts. That's a legitimate way how to build them, it's not a bug in Both blocks are using (and bundling) the We could configure webpack to create a shared chunk for To make it work reliably, the context would have to live in its own library, its own WordPress script. The reference in the blocks would have to be externalized, and But the very first thing you should look at is the Gutenberg block context API, which lets you propagage a selected attribute of a parent block to its children. |
Description
I create two block name 'parent' and 'child' by
npx @wordpress/create-block
add context provider on parent block with value, but inner block child block cannot show parent block context values.Step-by-step reproduction instructions
cd /wp-content/plugins
npx @wordpress/create-block --namespace=Custom --title="Test Context" context-test
cd context-test/src
open .
mkdir context-parent
mv * context-parent
npx @wordpress/create-block --namespace=Custom --title="Context child" context-child --no-plugin
touch utils.js
cd context-parent
block.json name to "custom/context-parent"
Screenshots, screen recording, code snippet
Here is GitHub public repo link: https://github.com/EmranAhmed/gutenberg-context-issue
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: