Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Update creating-dynamic-blocks.md #17481

Closed
wants to merge 2 commits into from
Closed

Docs: Update creating-dynamic-blocks.md #17481

wants to merge 2 commits into from

Conversation

thomasverleye
Copy link

The save function needs to be included in this example else the user will get an error in the inspector:

The "save" property must be specified and must be a valid function.

Description

Added a obligated save function.

How has this been tested?

Please view screenshots.

Screenshots

Before:
Screenshot inspector before

After
Screenshot Inspect after

Types of changes

Update documentation code example to make sure users learn Gutenberg without any errors in the inspector.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

The save function needs to be included in this example else the user will get an error in the inspector:
```
The "save" property must be specified and must be a valid function.
```
Copy link
Member

@mkaz mkaz left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution. I added one suggestion for consistency, but looks good

@gziolo
Copy link
Member

gziolo commented Sep 19, 2019

Looking at the screenshots I bet you are using an older version of Gutenberg:
https://www.npmjs.com/package/@wordpress/blocks/v/6.2.5

The npm package for @wordpress/blocks@6.2.5 was published over 4 months ago.

It might not include the changes introduced in #14510 which makes save optional when registering a block. Many core blocks don't have save provided and they work properly for months now. I don't think this change in documentation is expected.

@thomasverleye
Copy link
Author

It might not include the changes introduced in #14510 which makes save optional when registering a block.

Alright thanks for clearing this out. I guess the copy in this documentation should be updated as well then:

For many dynamic blocks, the save callback function should be returned as null, which tells the editor to save only the block attributes to the database. These attributes are then passed into the server-side rendering callback, so you can decide how to display the block on the front end of your site. When you return null, the editor will skip the block markup validation process, avoiding issues with frequently-changing markup.

Also in one of the further tutorials (meta boxes) has a code-snippet with save functions returning null as well.

https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/tutorials/metabox/meta-block-3-add.md

@thomasverleye
Copy link
Author

I've removed the save functions which return null and updated the documentation where it was stating the return function should be null.

@@ -189,14 +189,14 @@ registerBlockType( 'gutenberg-examples/example-05-dynamic', {
icon: 'megaphone',
category: 'widgets',

edit: function( props ) {
edit: ( props ) => {
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for changing this definition? The old one follows the WordPress coding standards. You could also use the following:

Suggested change
edit: ( props ) => {
edit( props ) {

Copy link
Member

@mkaz mkaz Sep 20, 2019

Choose a reason for hiding this comment

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

I would love to see clarification on proper standard for function definition. The coding guidelines here does not include anything: https://developer.wordpress.org/block-editor/contributors/develop/coding-guidelines/

I'm unclear if edit: function( props ) { } is preferred to edit: ( props ) => { }

If that is the case, then wouldn't it follow that const varName = function(props) { } would be preferred over const varName = ( props ) => { }

A lot of code across Gutenberg uses the shorter fat arrow syntax, which also seems to be the direction of the larger JS community.

Copy link
Member

Choose a reason for hiding this comment

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

I think the difference also exists between ES5 format, and ESNext format.

Copy link
Member

Choose a reason for hiding this comment

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

We have this ESLint rule enabled:
https://eslint.org/docs/rules/object-shorthand

See:

'object-shorthand': 'error',

Which favours the following:

/*eslint-env es6*/

// properties
var foo = {x, y, z};

// methods
var foo = {
    a() {},
    b() {}
};

I guess between a regular function and the fat arrow syntax you can't force one as they have some differences in how they behave:

  • regular function is hoisted
  • arrow function inherits scope from the parent

In general, I'd love us to find a way to start linting code examples in some way. I don't know how, but it feels like this would make it easier to review all documentation changes :)

return (
<ServerSideRender
block="gutenberg-examples/example-05-dynamic"
attributes={ props.attributes }
/>
);
},
}
Copy link
Member

Choose a reason for hiding this comment

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

This should stay as is, it follows the WordPress coding standards for ESNext codebase.

@@ -7,7 +7,7 @@ There are two primary uses for dynamic blocks:
1. Blocks where content should change even if a post has not been updated. One example from WordPress itself is the Latest Posts block. This block will update everywhere it is used when a new post is published.
2. Blocks where updates to the code (HTML, CSS, JS) should be immediately shown on the front end of the website. For example, if you update the structure of a block by adding a new class, adding an HTML element, or changing the layout in any other way, using a dynamic block ensures those changes are applied immediately on all occurrences of that block across the site. (If a dynamic block is not used then when block code is updated Guterberg's [validation process](https://developer.wordpress.org/block-editor/developers/block-api/block-edit-save/#validation) generally applies, causing users to see the validation message, "This block appears to have been modified externally").

For many dynamic blocks, the `save` callback function should be returned as `null`, which tells the editor to save only the [block attributes](https://developer.wordpress.org/block-editor/developers/block-api/block-attributes/) to the database. These attributes are then passed into the server-side rendering callback, so you can decide how to display the block on the front end of your site. When you return `null`, the editor will skip the block markup validation process, avoiding issues with frequently-changing markup.
For many dynamic blocks, there are no `save` callback function assigned, which tells the editor to save only the [block attributes](https://developer.wordpress.org/block-editor/developers/block-api/block-attributes/) to the database. These attributes are then passed into the server-side rendering callback, so you can decide how to display the block on the front end of your site.
Copy link
Member

Choose a reason for hiding this comment

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

Technically speaking, there is save function used which by default returns null. I agree that we should update this paragraph but we should make it more precise to avoid confusion.

@gziolo gziolo added [Type] Developer Documentation Documentation for developers First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Sep 23, 2019
@thomasverleye thomasverleye deleted the patch-1 branch January 8, 2020 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants