I am following the tutorial “Build your first block” and have a folder structure representing the scaffold created by the npx @wordpress/create-block@latest
command.
Using it as my starting point, I’m trying to retrieve the WordPress core store to add an entity, but I keep getting the error “Uncaught TypeError: Cannot read properties of null (reading ‘addEntities’)”.
Here is the whole src/index.js
file with the code I have added starting at import { dispatch } from '@wordpress/data';
(scaffold comments removed for brevity).
import { registerBlockType } from '@wordpress/blocks';
import './style.scss';
import Edit from './edit';
import metadata from './block.json';
registerBlockType( metadata.name, {
/**
* @see ./edit.js
*/
edit: Edit,
} );
import { dispatch } from '@wordpress/data';
dispatch( 'core' ).addEntities( [
{
name: 'programs',
kind: 'amilia/v1',
baseURL: 'amilia/v1/programs',
},
] );
It appears dispatch( 'core' )
returns null
and I have no idea why. Should I be calling it in a different place?