Sunday, September 25, 2022
HomeWordPress Developmentjavascript - Wordpress Gutenberg: Attribute overwritten by block duplicate

javascript – WordPress Gutenberg: Attribute overwritten by block duplicate


For a customized wordpress gutenberg block I’ve created a listing of buttons in order that customers can add/take away as many as wanted. Whereas this works fairly high-quality one situation happens: When duplicating a block the duplicate overwrites the array values of the origin block so long as we’re in enhancing mode. After saving and refreshing the web page the array values grow to be unbiased. Is anybody conscious of that behaviour?

// Simplified code:

// Attributes definition:
...
attributes: {
  buttons: {
    sort: 'array',
    default: []
  }
}
...
const buttons = props.attributes.buttons
...
// Edit inspector controls:
buttons.size > 0 && buttons.map( operate( button, index ) {
  return el(
    TextControl,
    {
      label: 'Textual content',
      worth: button.textual content,
      onChange: operate( newText ) {
        let newButtons = [ ...buttons ]
        newButtons[ index ][ 'text' ] = newText

        props.setAttributes( { buttons: newButtons } )
      }
    }
  )
}

// Edit / Save output:    
buttons.size > 0 && buttons.map( operate( button, index ) {
  return el(
    'a',
    {
      key: 'button-' + index        
    },
    button.textual content
  )
} )

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments