admin管理员组文章数量:1310004
I managed to get an attribute working in my plugin. Well, sort of. I can read and write the attribute props.attributes.content . But I get the message in the Gutenberg block itself:
Error loading block: Invalid parameter(s): attributes
And in the network inspector I see a 400:
data: {status: 400, params: {attributes: "content is not a valid property of Object."}} params: {attributes: "content is not a valid property of Object."}
Here is the relevant code:
registerBlockType('simpletoc/toc', {
title: __('SimpleTOC', 'simpletoc'),
icon: simpletocicon,
category: 'layout',
attributes: {
content: {
type: 'string',
source: 'text',
selector: 'div',
},
},
edit: function(props) {
props.setAttributes( { content: "newContent" } );
console.info(props.attributes.content);
const mycontent = props.attributes.content;
return (
<div>
<InspectorControls>
<ToggleControl
label={mycontent}
/>
</InspectorControls>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
className="components-icon-button components-toolbar__control"
label={__('Update table of contents', 'simpletoc')}
onClick={function() {
sendfakeAttribute(props)
}}
icon="update"
/>
</ToolbarGroup>
</BlockControls>
<p className={props.className}>
<ServerSideRender block={props.name} attributes={props.attributes} />
</p>
</div>
)
},
save: props => {
return null;
},
});
But I can write the attribute with
props.setAttributes( { content: "newContent" } );
with console.info and my ToggleControl I can actually read the value! What is going on?
I managed to get an attribute working in my plugin. Well, sort of. I can read and write the attribute props.attributes.content . But I get the message in the Gutenberg block itself:
Error loading block: Invalid parameter(s): attributes
And in the network inspector I see a 400:
data: {status: 400, params: {attributes: "content is not a valid property of Object."}} params: {attributes: "content is not a valid property of Object."}
Here is the relevant code:
registerBlockType('simpletoc/toc', {
title: __('SimpleTOC', 'simpletoc'),
icon: simpletocicon,
category: 'layout',
attributes: {
content: {
type: 'string',
source: 'text',
selector: 'div',
},
},
edit: function(props) {
props.setAttributes( { content: "newContent" } );
console.info(props.attributes.content);
const mycontent = props.attributes.content;
return (
<div>
<InspectorControls>
<ToggleControl
label={mycontent}
/>
</InspectorControls>
<BlockControls>
<ToolbarGroup>
<ToolbarButton
className="components-icon-button components-toolbar__control"
label={__('Update table of contents', 'simpletoc')}
onClick={function() {
sendfakeAttribute(props)
}}
icon="update"
/>
</ToolbarGroup>
</BlockControls>
<p className={props.className}>
<ServerSideRender block={props.name} attributes={props.attributes} />
</p>
</div>
)
},
save: props => {
return null;
},
});
But I can write the attribute with
props.setAttributes( { content: "newContent" } );
with console.info and my ToggleControl I can actually read the value! What is going on?
Share Improve this question asked Jan 6, 2021 at 21:58 MarcMarc 6979 silver badges28 bronze badges 1- Are you sure there is a defined content attribute at the time of render? You set the attribute in the first line, but that doesn't mean the attribute is instantly updated. It likely means that React will render the component again because the props changed, it may not apply until after rendering. Have you considered setting a default value? – Tom J Nowell ♦ Commented Jan 6, 2021 at 23:15
1 Answer
Reset to default 4The error in question is likely because when you run register_block_type()
, you didn't set the block attributes or that it (which is an array in PHP) doesn't have the attribute named content
.
So make sure the attributes are defined in both JavaScript (via registerBlockType()
) and PHP (via the above-mentioned function), and that the schema is valid. E.g.
In JS:
registerBlockType( 'simpletoc/toc', { attributes: { content: { type: 'string', // other args }, // other attributes }, // ... } );
In PHP:
register_block_type( 'simpletoc/toc', array( 'attributes' => array( 'content' => array( 'type' => 'string', // other args ), // other attributes ), // ... ) );
本文标签: pluginsGutenberg Error loading block Invalid parameter(s) attributes but it can be used in the code
版权声明:本文标题:plugins - Gutenberg: Error loading block: Invalid parameter(s): attributes but it can be used in the code 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741855528a2401318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论