• Resolved lewiswharf

    (@accescape)


    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 9 months, 2 weeks ago by lewiswharf.
    • This topic was modified 9 months, 2 weeks ago by lewiswharf. Reason: code formatting
Viewing 1 replies (of 1 total)
  • Thread Starter lewiswharf

    (@accescape)

    Ah, need to put dispatch code in a hook…

    import { dispatch } from '@wordpress/data';
    import { addAction } from '@wordpress/hooks';
    
    addAction('plugins.pluginRegistered', 'amilia/register-programs-entity', (() => {
    	dispatch( 'core' ).addEntities( [
    		{
    			name: 'program',
    			kind: 'amilia',
    			baseURL: '/amilia/v1/programs',
    
    		},
    	] );
    }));
Viewing 1 replies (of 1 total)
  • The topic ‘wp.data.dispatch( ‘core’ ) returns null’ is closed to new replies.