Gerald Ibra
Gerald Ibra

Reputation: 111

Passing attributes from parent to child Gutenberg Blocks

How to pass the parent attributes or to pass some props in the inner child block?

<InnerBlocks allowedBlocks={[ 'groupama-blocks/block-08']}/>

For example I'd like to add this block attributes to block 08 that I will add as a inner block.

Upvotes: 1

Views: 4660

Answers (2)

Taj
Taj

Reputation: 41

Block context is a Wordpress feature used in "registerBlockType", which allows ancestor blocks to provide values to descendant blocks.

https://developer.wordpress.org/block-editor/reference-guides/block-api/block-context/

Upvotes: 4

Gerald Ibra
Gerald Ibra

Reputation: 111

PASSING DATA FROM PARENT TO CHILD GUTENBERG

PARENT

const {useSelect, dispatch, select} = wp.data;

edit: (props) => {
    const {
        clientId
    } = props;

    var children = select('core/block-editor').getBlocksByClientId(clientId).[0].innerBlocks;
    children.forEach(function(child){
        dispatch('core/block-editor').updateBlockAttributes(child.clientId, {label: 'Hello'})
    });

CHILD

let parent = select('core/block-editor').getBlockParents(clientId);
const parentAttributes = select('core/block-editor').getBlockAttributes(parent);
console.log(parentAttributes); // Block Attributes
console.log(props.attributes.label); // Passing Props

Upvotes: 7

Related Questions