Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/designers-developers/developers/block-api/block-edit-save.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ edit( { attributes, setAttributes, className, isSelected } ) {
}
```

When calling `setAttributes`, it's important that attributes are not mutated in the process of updating them. This is considered bad practice, the core of Gutenberg has been written with the same philosophies as Redux and state is treated as immutable data. It can also cause bugs. Objects and arrays are passed as references in JavaScript, so mutating them can affect other bindings to those references, for example an attribute's default value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a lot of sense to document but I think the lingo might be slightly over the head of many WP developers.

It's obviously good to introduce and use higher level terms so folks can learn and expand their skills, but maybe this can be expanded with some extra context, definitions, etc.?

(This could also link into @mkaz's work on a JS tutorial, so we could point someone to some additional background.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @chrisvanpatten—I've simplified the text a bit, tried using a simpler vocabulary.


```js
// Good - here a new array is created from the old list attribute and a new list item:
const { list } = attributes;
const addListItem = ( newListItem ) => setAttributes( { list: [ ...list, newListItem ] } );

// Bad - here the list from the existing attributes is mutated to add the new list item:
const { list } = attributes;
const addListItem = ( newListItem ) => {
list.push( newListItem );
setAttributes( { list } );
};

```


## Save

The `save` function defines the way in which the different attributes should be combined into the final markup, which is then serialized by Gutenberg into `post_content`.
Expand Down