首页 > 解决方案 > 将属性从父级传递给子级 Gutenberg Blocks

问题描述

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

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

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

标签: wordpressattributesparent-childwordpress-gutenberggutenberg-blocks

解决方案


将数据从父母传递给孩子 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

推荐阅读