Explanation of the issues in your code:
-
The main one which causes the block editor to crash, is because you used
InnerBlocksin thesavefunction. You should instead useInnerBlocks.Content.See this and this for more details on
InnerBlocksusage, accepted props, etc. -
As for the
getAttributeerror, it’s because you did not useuseBlockPropsin theeditfunction (which should also be used in thesavefunction).And why should you use
useBlockProps, is because when you registered the block type in JavaScript (usingregisterBlockType()), you didn’t set theapiVersionproperty, hence Gutenberg uses theapi_versionvalue (which is 2) you set via PHP (usingregister_block_type()), and when using the block API version 2,useBlockPropsshould be used, so that the block is properly wrapped, centered and “clickable”.
So to fix the issues:
-
At the top in your script, replace the
const { InnerBlocks } = wp.blockEditor;with:const { InnerBlocks, useBlockProps } = wp.blockEditor; -
Use
useBlockProps()in theeditfunction, anduseBlockProps.save()andInnerBlocks.Contentin thesavefunction:edit() { const blockProps = useBlockProps( { style: blockStyle } ); return ( <div { ...blockProps }><InnerBlocks /></div> ); }, save() { const blockProps = useBlockProps.save( { style: blockStyle } ); return ( <div { ...blockProps }><InnerBlocks.Content /></div> ); }
Additional Notes
-
Are you already using the
@wordpress/scriptspackage to build your scripts (see JavaScript build setup in the block editor handbook)?Because if you are, you wouldn’t need to manually specify the dependencies for your script, which is
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' )in the question.However, you will need to use the
importstatement and notconstwhen loading the dependencies in your script. So for example:// Instead of using "const": const { InnerBlocks, useBlockProps } = wp.blockEditor; // Use "import": import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
In response to your comment…
if I replace
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor' )with justarray()or any other combination,
excluding any of them or all of them, I still get an error
The following excerpt might help:
Here is how to use this asset file to automatically set the dependency
list for enqueuing the script. This prevents having to manually update
the dependencies, it will be created based on the package imports
used within your block.$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php'); wp_register_script( 'myguten-block', plugins_url( 'build/index.js', __FILE__ ), $asset_file['dependencies'], $asset_file['version'] );