I've created a custom block that uses a WordPress core block as an innerBlock child; more specifically the core/embed block. What I'd like to do is listen for changes in the child block's attributes, so that I can respond to them in the parent block.
The custom block edit function returns the following:
return (
<div { ...useBlockProps() } onChange={parentUpdate}>
<InnerBlocks
allowedBlocks={['core/embed']}
template={EMBED_TEMPLATE}
templateLock="all"
></InnerBlocks>
</div>
);
And the parentUpdate
function currently looks like:
const parentUpdate = () => {
const thisBlock = select('core/editor').getBlocksByClientId(clientId)[0]
const childBlock = thisBlock.innerBlocks[0]
const videoURL = childBlock.attributes?.url
console.log(videoURL)
}
The parentUpdate
function fires whenever the url attribute in the child embed block changes but (perhaps unsurprisingly) it fires before the child event block updates, meaning I get the value prior to the change, not after.
How should I be approaching this, so that I can use the child attributes after the child block updates?
onChange
isn't a block related event, it's a DOM node related event, I do not believe you can do this via props and attributes in the component as that's not how block data works,<InnerBlocks>
is not the source of data for inner blocks, it's just the mechanism by which they're displayed, it gets that data from elsewhere. What are you trying to implement that requires this? What is your block supposed to do? There's no context provided here so it's difficult to advise on the appropriate actions to takeuseSelect
considered. I would also say that all the things you would apply this to actually have very different solutions and this is the optimal solution for none of them