-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add: note in the save function documentation to discourage side effects #15765
Add: note in the save function documentation to discourage side effects #15765
Conversation
It seems to be a good clarification to be added. |
docs/designers-developers/developers/block-api/block-edit-save.md
Outdated
Show resolved
Hide resolved
@@ -226,6 +226,13 @@ For most blocks, the return value of `save` should be an [instance of WordPress | |||
|
|||
_Note:_ While it is possible to return a string value from `save`, it _will be escaped_. If the string includes HTML markup, the markup will be shown on the front of the site verbatim, not as the equivalent HTML node content. If you must return raw HTML from `save`, use `wp.element.RawHTML`. As the name implies, this is prone to [cross-site scripting](https://en.wikipedia.org/wiki/Cross-site_scripting) and therefore is discouraged in favor of a WordPress Element hierarchy whenever possible. | |||
|
|||
_Note:_ The save function should be a pure function that depends only on the attributes used to invoke it. | |||
It can not have any side effect or retrieve information from another source, e.g. it is not possible to use the data module inside it `wp.data.select...`. |
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 mention of "wp.data.select...
" could help clarify what's referred to, but for what it's worth, we should discourage this use of the window global in blocks anywhere, since it cannot account for separate data registries, which will become necessary for e.g. embedded editors (#7119), multiple editors on a page.
docs/designers-developers/developers/block-api/block-edit-save.md
Outdated
Show resolved
Hide resolved
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
Co-Authored-By: Andrew Duthie <andrew@andrewduthie.com>
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.
A nice clarification 👍
Description
I noticed people are not aware side effects cannot be used in the save function, and people use the data module in the save function e.g: to query the post title. This may easily break the blocks.
This PR updates the documentation to make people more aware of this problem.