将属性从父级传递到子级古腾堡块

时间:2021-04-01 09:12:00

标签: wordpress attributes parent-child wordpress-gutenberg gutenberg-blocks

如何在内部子块中传递父属性或传递一些道具?

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

例如,我想将此块属性添加到我将作为内部块添加的块 08。

1 个答案:

答案 0 :(得分:0)

将数据从父母传递给孩子 GUTENBERG

家长

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'})
    });

孩子

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