I have registered a Gutenberg block using the "registerBlockType" function. Within that function I declare attributes for the block like so:

registerBlockType( 'myblock/myblock', {

    attributes: {
        seed: {
            type: 'string'
        compositionData: {
            type: 'string'
    {other wordpress related settings...}

I then pass the attributes into my edit function, and edit them as such:

function Edit( { attributes, setAttributes  } ) {
    setAttributes( { seed: 5 } );

This all works as expected and attributes.seed is now my random number (assuming I update the post).

The problem however is if I have 2 of "myblocks" on the same post/page, whenever I edit one of them, the other gets edited also. So they will always both share the same seed, and I have no way to differentiate them.

  • Do you need a fixed seed that is unique? Or can you just use a random one? – kero Dec 18 '20 at 10:06
  • @kero, This is just an example, really I am saving a large JSON string that I use to configure the block. However the JSON string is currently being shared between all the blocks on the page, and I definitely need this string to be unique and editable for each block. – YAHsaves Dec 18 '20 at 10:09
  • Ok, let me ask again to make sure I fully understand: You have some routine to get a default state. This routine should check for other existences of the block on the same page and return unique default states for each block. Right? – kero Dec 18 '20 at 10:19
  • Um no, I thought when I register a block and set attributes for it they all get tied into that specific block. It is in my end to keep track of the attributes? If so what tools do I have available to do that with? – YAHsaves Dec 18 '20 at 10:57
  • @kero Also I thought I set the default state of the attribute in the registerBlockType function – YAHsaves Dec 18 '20 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.