2

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?

2
  • I'm having the same exact problem with useSelect. Are you using create-guten-block as well? 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
    Oct 24, 2019 at 20:23

2 Answers 2

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

0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.