wp.data.dispatch( ‘core’ ) returns null

[ad_1]

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?

  • This topic was modified 3 hours, 54 minutes ago by lewiswharf.
  • This topic was modified 3 hours, 53 minutes ago by lewiswharf. Reason: code formatting

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer