• Resolved luizbriganti

    (@luizbriganti)


    I’m trying to develop a custom Gutenberg block, and since it’s likely to insert several of these blocks in the same page which perform different tasks saving different values, I need too get the index of each block in the page. I’ve tried to get it with js, but since scripts are loaded befor the block, the result is null, undefined or error. Is there a way to get this index?

    Thanks.

Viewing 1 replies (of 1 total)
  • Hi @luizbriganti,

    You can use Gutenberg’s Data API to do it. This will get you a list of all the blocks:

    wp.data.select('core/block-editor').getBlocks();

    You can run it in your browser console to see the output. Now, if you want to make sure this runs each time the block is edited, you can use it with useSelect hook or subscribe, like here: https://github.com/WordPress/gutenberg/tree/master/packages/data#subscribe

    And if you want to get the position of a particular block, you can get the clientID of that block and match it in the list.

    Hope this points you in the right direction!

Viewing 1 replies (of 1 total)
  • The topic ‘Get index of Gutenberg block’ is closed to new replies.