I’ve been having fun working heavily with Gutenberg over the last several weeks. As I ran into a couple things, I started tracking them in a draft post so I could work through and publish them later.
Here’s the first set. đ
How to access className from a block’s save() function
The classname
 attribute is available to a block’s edit()
 function by default, but it is not available to the block’s save()
 function. I learned from this GitHub issue that registering className
 as one of the block’s attributes will provide it to the save()
 function.
registerBlockType( 'my/block', { attributes: { className: { type: 'string', default: '', }, }, edit( props ) { // className via props const { attributes, className } = props; const { customData } = attributes; return ( // ... ); }, save( { attributes } ) { // className via attributes const { className, customData } = attributes; return ( // ... ); }, } );
Be careful importing Lodash modules
If you want to use something from Lodash, like find()
:
// Import the module like this import find from 'lodash/find'; // Not like this... import { find } from 'lodash';
The latter brings in the global _
from Lodash, which overrides the global _
provided by Underscore and causes trouble.
A this.activateMode is not a function
error that prevented media blocks from being used led me to this issue, which helped me.
Beware of non-changing arrays
I ran into this a couple different ways recently and I’m still working on understanding it completely, but things like Array().push and Array().pop modify the reference value of an existing array and may not trigger the state change you’re expecting.
let apples = []; let oranges = apples; oranges; // [] apples.push( 1 ); oranges; // [ 1 ] oranges === apples; // true oranges === apples.concat( 1 ); //false
I picked up on better approaches for this via this issue when a block’s attributes weren’t saving as expected.
The only requirement for your mention to be recognized is a link to this post in your post's content. You can update or delete your post and then re-submit the URL in the form to update or remove your response from this page.
Learn more about Webmentions.