Looking through older tutorials or code for blocks built in 2018/2019 vs the more current blocks, I see two different ways attribute values are set.
For example, the “old” way for an attribute called “content” might be:
onChange={ (newContent) => setAttributes({ content: newContent })}
Whereas in a more modern block:
onChange={ ( content ) => setAttributes( { content } )}
I get it’s a minor change but I’m curious if anyone here knows why.
Thanks!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Because { content } is shorthand for { content: content } it’s a JS thing not a WordPress thing.
Both methods work and do the same thing, but the { content } syntax is shorter, so the variable was named content in newer examples to allow it.
Otherwise, there are no security or performance improvements, it’s just a shorter syntax for the same thing.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0