skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search