Skip to content

Request to document how to declare dependency to to things like wp-element and wp-data so wp.data.select('core/editor') is not undefined while editor starts #13530

Open
@websupporter

Description

Describe the bug
Using wp.data.select('core/editor').getBlocks() or other methods of core/editor in the wp.data.subscribe() ends up in a white screen, when opening the editor. typeof wp.data.select('core/editor') returns at some point of the startup undefined.

To Reproduce
Steps to reproduce the behavior:

  1. Activate the following plugin:

plugin.php:

<?php
/**
 * Plugin Name: Test
 */
add_action('enqueue_block_editor_assets', 'test_script');
function test_script() {
    wp_enqueue_script(
        'script',
        plugins_url( '/script.js', __FILE__ ),
        array( 'wp-data' )
    );
}

script.js:

wp.data.subscribe(
    () => {
        console.log(wp.data.select('core/editor').getBlocks().length);
    }
);
  1. open editor
  2. see white screen.

Expected behavior
The editor should show up and the number of blocks should be printed correctly in the console.

Screenshots
grafik

Desktop (please complete the following information):

  • OS: Ubuntu 14.04
  • Browser Chrome / Firefox

Additional context

  • WordPress 5.0.3

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions