-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Block Editor: Support passing updater function to 'setAttributes' #69709
Conversation
I wasn't able to find any prior discussion about this pattern, so I would love to hear feedback from @WordPress/gutenberg-core |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +18 B (0%) Total Size: 1.85 MB
ℹ️ View Unchanged
|
If there are no objections, I'm going to merge this DX improvement. |
It would be good to revisit the existing docs as part of the effort: In particular, I was surprised that the example included covers the case that potentially could be expressed with the the updater function: const toggleSetting = () => setAttributes( { mySetting: ! mySetting } ); Therefore, some best practices like in React docs would be useful as the first step. |
Added a new documentation section in c8fb55b. |
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 really like that this brings it closer to how reacts setState also allows the callback function :) Nice addition!
@@ -135,6 +135,22 @@ const addListItem = ( newListItem ) => { | |||
|
|||
Why do this? In JavaScript, arrays and objects are passed by reference, so this practice ensures changes won't affect other code that might hold references to the same data. Furthermore, the Gutenberg project follows the philosophy of the Redux library that [state should be immutable](https://redux.js.org/faq/immutable-data#what-are-the-benefits-of-immutability)—data should not be changed directly, but instead a new version of the data created containing the changes. | |||
|
|||
The `setAttribute` also supports an updater function as an argument. It must be a pure function, which takes current attributes as its only argument and returns updated attributes. This method is helpful when you want to update an value based on a previous state or when working with objects and arrays. |
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.
Cool addition! It would help to add a note about WordPress version when it's going to be available. Otherwise, it might create confusion as it won't work as expected with older versions.
What?
PR updates
setAttributes
method to support an updater function, similar to theuseState
setter.Usage:
Why?
This should make it easier to work with complex attributes (
style
,query
), especially in cases where attributes need to be updated inside an effect or when callback needs to be memoized - for example, to debounce the updates.gutenberg/packages/block-library/src/query/edit/query-content.js
Lines 85 to 91 in b28716a
Testing Instructions
Testing Instructions for Keyboard
Same.