• Hi all,

    I’m fairly new to working with Blocks. I have previously managed to make a plugin that changed my post’s meta values, however I’m now trying to make something a lot simpler and it’s just refusing to update the data in the backend.

    You can find source code for my block here. It simply adds a toggle to set a meta value to false or true.

    While the value to my attribute seems to change after calling:
    setAttributes( { featured: value } );

    If I save my post and check the rest api, the value actually won’t have changed.

    I have absolutely no clue why this isn’t working. Is there anything wrong with the code?

    • This topic was modified 4 years, 8 months ago by creativiii.
    • This topic was modified 4 years, 8 months ago by creativiii.
Viewing 6 replies - 1 through 6 (of 6 total)
  • It looks like the problem is the mix of setState and setAttributes.

    Take a look at this example:

    edit({ attributes, setAttributes }) {
    
      function onChangeCheckboxField( ) {
        let toggledValue=(attributes.isNew===true)?false:true;
        setAttributes( { isNew: toggledValue } );
      }
    
      return (
        <div className="container de_title">
          <PlainText
            onChange={ content => setAttributes( { price: content } ) }
            value={ attributes.price }
            placeholder="price"
            className="price"
          />
        </div>
      );
    },
    Thread Starter creativiii

    (@creativiii)

    That’s also what I thought, so I changed it to just use Attributes rather than a mix, but the problem is still the same.

    I’ve updated the github.

    <FeaturedRadio featured={attributes.featured}/>

    Do you need to pass attributes.featured since it should already be accessible to the function?

    Thread Starter creativiii

    (@creativiii)

    Updated the Github, that wasn’t the problem either

    Thread Starter creativiii

    (@creativiii)

    I figured it out. It seems that at some point I added another custom block and used the same name for it.

        wp_enqueue_script(
            'palette-script', // this was already used
            plugins_url( 'build/index.js', __FILE__ ),
            array( 'wp-blocks', 'wp-element', 'wp-components' )
        );
    

    So even though they were in completely different files and my Block didn’t give me any errors, it was actually conflicting with the way it was working.

    Thread Starter creativiii

    (@creativiii)

    Upon second inspection I can set the attributes to true, but I can’t set it to false after.

    That’s really strange and doesn’t make a whole lot of sense to me.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘setAttributes doesn’t save attributes’ is closed to new replies.