I'm trying to pass the clientId from the parent block to a child block. I need to stuff the clientId into an attribute value. When I use setAttribute trying to assign the value to sliderId, I get the following warning:
Warning: Cannot update a component (`Unknown`) while rendering a different component (`Test2Edit`).
What is the Gutenberg (JSX) way to solve this?
My test block is as follows:
gc-x-test/block.json
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "give-camp/gc-x-test",
"version": "0.1.0",
"title": "GC Test",
"category": "text",
"icon": "slides",
"description": "Generic test",
"attributes": {
"sliderId": { "type": "string", "default": "s-01" }
},
"supports": {
"type": "object",
"anchor": true
},
"textdomain": "give-camp-blocks",
"editorScript": "file:./index.js"
}
gc-x-test/index.js
import {
registerBlockType
} from '@wordpress/blocks';
import metadata from './block.json';
import {
useBlockProps
} from '@wordpress/block-editor';
//
function Test2Edit(props) {
//
const {
attributes: { sliderId },
setAttributes,
clientId,
} = props;
setAttributes( { sliderId: clientId } ); // see providesContext
const blockProps = useBlockProps( );
//
return (
<div
{ ...blockProps }
>
<div>1 of ?</div>
</div>
);
//
};
//
function Test2Save( props ) {
//
const blockProps = useBlockProps.save( );
return (
<div
{ ...blockProps }
>
<div>1 of ?</div>
</div>
);
//
};
//
registerBlockType( metadata.name, {
apiVersion: metadata.apiVersion,
title: metadata.title,
supports: metadata.supports,
attributes: metadata.attributes,
//
edit: Test2Edit,
save: Test2Save,
} );