-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conversation
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. ```
There was a problem hiding this 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
docs/designers-developers/developers/tutorials/block-tutorial/creating-dynamic-blocks.md
Outdated
Show resolved
Hide resolved
Looking at the screenshots I bet you are using an older version of Gutenberg: The npm package for It might not include the changes introduced in #14510 which makes |
Alright thanks for clearing this out. I guess the copy in this documentation should be updated as well then:
Also in one of the further tutorials (meta boxes) has a code-snippet with save functions returning |
…ons are mentioned
I've removed the save functions which return |
@@ -189,14 +189,14 @@ registerBlockType( 'gutenberg-examples/example-05-dynamic', { | |||
icon: 'megaphone', | |||
category: 'widgets', | |||
|
|||
edit: function( props ) { | |||
edit: ( props ) => { |
There was a problem hiding this comment.
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:
edit: ( props ) => { | |
edit( props ) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 } | ||
/> | ||
); | ||
}, | ||
} |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
The save function needs to be included in this example else the user will get an error in the inspector:
Description
Added a obligated
save
function.How has this been tested?
Please view screenshots.
Screenshots
Before:
After
Types of changes
Update documentation code example to make sure users learn Gutenberg without any errors in the inspector.
Checklist: