【问题标题】:"TypeError: Cannot read property 'innerBlocks' of null" for InnerBlocks in Gutenberg古腾堡 InnerBlocks 的“TypeError:无法读取 null 的属性‘innerBlocks’”
【发布时间】:2019-05-16 11:17:38
【问题描述】:

我最近问了一个关于同一topic 的问题。在那里我自己发现了这个问题。但不幸的是,我遇到了一个新错误:

TypeError: Cannot read property 'innerBlocks' of null
    at InnerBlocks.synchronizeBlocksWithTemplate (script.build.js?ver=1:149405)
    at InnerBlocks.componentDidMount (script.build.js?ver=1:149366)
    at zc (react-dom.min.js?ver=16.6.3:146)
    at wc (react-dom.min.js?ver=16.6.3:138)
    at fa (react-dom.min.js?ver=16.6.3:137)
    at ng (react-dom.min.js?ver=16.6.3:149)
    at Se (react-dom.min.js?ver=16.6.3:40)

所以我用相同的代码尝试了它,但我没有让它工作:

const {registerBlockType} = wp.blocks;
const {InspectorControls, RichText, MediaUpload} = wp.editor;

import {TextControl} from '@wordpress/components';

import {InnerBlocks} from '@wordpress/editor';

let blockStyle = {
    marginTop: "25px",
    marginBottom: "25px;"
};

registerBlockType('myself/test-component', {
    title: 'Test component',
    icon: 'editor-insertmore',
    category: 'common',
    attributes: {
        title: {
            type: 'string'
        }
    },

    edit(props) {
        const {setAttributes, attributes} = props;

        function setTitle(changes) {
            setAttributes({
                title: changes
            })
        }

        return (
            <div style={blockStyle}>
                <TextControl
                    placeholder="Titel"
                    value={attributes.title}
                    onChange={setTitle}
                />
                <InnerBlocks
                    templateLock={false}
                    renderAppender={(
                        () => <InnerBlocks.ButtonBlockAppender/>
                    )}
                />
            </div>
        )
    },

    save(props) {
        const {attributes, className} = props;

        return (
            <div style={blockStyle}>
                <InnerBlocks.Content/>
            </div>
        );
    }
});

然后我检查了作为插件嵌入的内置 script.js 文件。我们有以下情况this.props.blocknull

key: "componentDidMount",
value: function componentDidMount() {
  var innerBlocks = this.props.block.innerBlocks; // only synchronize innerBlocks with template if innerBlocks are empty or a locking all exists

  if (innerBlocks.length === 0 || this.getTemplateLock() === 'all') {
    this.synchronizeBlocksWithTemplate();
  }

  if (this.state.templateInProcess) {
    this.setState({
      templateInProcess: false
    });
  }
}

更新

变量this.props有以下变量:

是否有人面临同样的问题或有解决方法?

【问题讨论】:

  • 我看不到你在哪里传递 'block' 道具。
  • @rrd 我尝试将属性设置为 InnerBlocks 组件,但没有成功。在构建过程之后,它看起来像一个内部prop。我用屏幕截图更新了我的问题,显示了this.props 对象。
  • 好吧 this.props.block 似乎是空的,所以 innerBlocks 永远不会在那里。这就是您收到错误的原因 - 实际上错误本身告诉您同样的情况。
  • @rrd,没错,我也注意到了。但是为什么是blocknull。除了本网站所述的这些属性外,我没有其他选项可以定义该对象:ibenic.com/enable-inner-blocks-gutenberg/#gist91608525

标签: javascript wordpress reactjs wordpress-gutenberg


【解决方案1】:

再次,我解决了我自己的问题。最后,这个问题对我来说并不清楚,但它确实有效。

我现在所做的只是将wp.editor 用作InnerBlocks 组件的导入实例。有了它,它现在加载了完整的 InnerBlocks 编辑器,我现在可以在其中添加自定义块。

const {registerBlockType} = wp.blocks;
const {InspectorControls, RichText, InnerBlocks} = wp.editor; //Imported the InnerBlocks from this source.

import {CheckboxControl, TextControl} from '@wordpress/components';

// Removed this line
// import {InnerBlocks} from "@wordpress/editor";

【讨论】:

    猜你喜欢
    • 2019-10-01
    • 1970-01-01
    • 2020-01-29
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2021-12-04
    • 2021-12-17
    相关资源
    最近更新 更多