我正在尝试为每个定制的gutenberg块获取一个唯一的id。我找到useInstanceId了。这是否可以与此语法一起使用?
registerBlockType('custom/block', {
title: 'Custom',
edit() {
return (<p>{unqiue-block-id}</p>)
},
save() {
return (<p>{unqiue-block-id}</p>)
},
});
发布于 2021-01-07 22:35:39
你可以用props
之外的clientId
来解决这个问题。不幸的是,它只存在于编辑功能的道具中。因此,您需要将clientId作为额外的属性提供。如果我理解正确的话,我自己很难用useInstanceId
完成这件事,因为你需要用HigherComponent来做。所以我选择了clientId
解决方案。
我最初的想法是从here得到的
下面是使用clientId
完成此任务所需的代码。
registerBlockType('custom/block', {
title: 'Custom',
attributes: {
yourId: {
type: 'string',
}
},
edit: props => {
const {
attributes: {
yourId,
},
clientId,
setAttributes,
} = props;
setAttributes({ yourId: clientId });
return (
<p>{yourId}</p>
);
},
save: props => {
const {
attributes: {
yourId,
},
} = props;
return (
<p>{yourId}</p>
);
},
});
https://stackoverflow.com/questions/65582451
复制相似问题