admin管理员组

文章数量:1208155

I have the @wordpress/data package installed, but I can't use useDispatch in my block edit function:

const { registerBlockType } = wp.blocks;
const { useDispatch, useSelect } = wp.data;
.
.
.
registerBlockType( 'rb-bootstrap/grid', {
    ...
    edit: function( props ) {
         const { replaceInnerBlocks } = useDispatch("core/block-editor");
    }
}

TypeError: useDispatch is not a function

What am I missing?

I have the @wordpress/data package installed, but I can't use useDispatch in my block edit function:

const { registerBlockType } = wp.blocks;
const { useDispatch, useSelect } = wp.data;
.
.
.
registerBlockType( 'rb-bootstrap/grid', {
    ...
    edit: function( props ) {
         const { replaceInnerBlocks } = useDispatch("core/block-editor");
    }
}

TypeError: useDispatch is not a function

What am I missing?

Share Improve this question asked Oct 23, 2019 at 4:38 RabaRaba 4062 silver badges12 bronze badges 2
  • I'm having the same exact problem with useSelect. Are you using create-guten-block as well? – Claudio Cortese Commented Oct 24, 2019 at 19:38
  • @claudio yes I am. Just in case i did reinstalled the @wordpress/data package, but the problem persisted. I ended up using withDispatch so not to keep wasting time, but it would be nice to have something more declarative like useDispatch – Raba Commented Oct 24, 2019 at 20:23
Add a comment  | 

2 Answers 2

Reset to default 0

I used to have the same problem. After I updated Wordpress, window.wp.data.useDispatch was available.

I'm guessing you also have an out of date Wordpress / Gutenberg

Stuff like wp.data.useDispatch and wp.data.useSelect were implemented in WP 5.3 (released on November 12, 2019). In older versions those are not available and you are supposed to use compose. This article details how it was on 5.2 and before, and how it is now (much cleaner code).

本文标签: plugin developmentGutenberg useDispatch is not a functionwordpressdata included