admin管理员组文章数量:1277342
I'm developing a custom plugin with react and I would like to create a unique identifier for each new added instance of my custom block as soon as it is added to the page.
So I add my "custom block" and the code runs once to create and assign the id. When I visit the backend again, the code should not run again. But if I add a new instance of my "custom block", the code runs once again for this instance and creates & assigns a new id. So I guess I'm looking for a hook like "blockAddedToPage" or so, but I can't find something matching.
I know that there is the clientid, but I need something permanent and shorter :D
I wrote a small helper function to create my id, but how do I run it only when the block is added to the page?
I'm developing a custom plugin with react and I would like to create a unique identifier for each new added instance of my custom block as soon as it is added to the page.
So I add my "custom block" and the code runs once to create and assign the id. When I visit the backend again, the code should not run again. But if I add a new instance of my "custom block", the code runs once again for this instance and creates & assigns a new id. So I guess I'm looking for a hook like "blockAddedToPage" or so, but I can't find something matching.
I know that there is the clientid, but I need something permanent and shorter :D
I wrote a small helper function to create my id, but how do I run it only when the block is added to the page?
Share Improve this question edited Sep 30, 2021 at 13:36 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Sep 30, 2021 at 12:38 LenitaLenita 112 bronze badges 3 |1 Answer
Reset to default 0You can do this with a block attribute and useEffect()
in your edit
function:
useEffect(() => {
// If the ID is already set, we don't need to do anything
if (attributes.id) {
return;
}
// The ID attribute isn't set, so we'll create it and set it now
setAttributes({
id: 123, // This is just for example purposes, you can set up your ID however you want
});
}, []); // Empty array ensures this effect only runs once
One thing to note: This will not run your ID creation code again if you copy/paste/duplicate an existing block with an existing ID attribute.
If you're looking for a more robust solution to generating a unique ID for a block (and are ok with the ID possibly changing), I used a similar technique (see code here) for a block of mine that combines the post ID and an instance ID to create the unique ID. If you rearrange the blocks, insert new ones, etc. it regenerates the IDs as necessary.
本文标签: plugin developmentRun code once when block is created
版权声明:本文标题:plugin development - Run code once when block is created 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292278a2370622.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
useEffect
, but there are times when that is definitely not the solution. So is watching the data store, filters, and it may be that you're headed down a dead end, a lot of people who ask this are trying to do something they shouldn't that leads to lots of new problems. Give us some context for what you're trying to do to make the question answerable, and eliminate possibilities – Tom J Nowell ♦ Commented Sep 30, 2021 at 13:24