reactjs - 在 block 编辑器中获取对 BlockListBlock 的引用

标签 reactjs wordpress wordpress-gutenberg

在 gutenberg 编辑器中,我需要获取 BlockListBlock 元素的引用,但它似乎不起作用。有办法实现吗?

const withCustomAttribute = createHigherOrderComponent(
  (BlockListBlock) => {
    return (props) => {
      const blockRef = createRef();

      useEffect(() => {
        console.log(blockRef); // => "{current: null}"
      });
      return <BlockListBlock {...props} ref={blockRef} />;
    };
  },
  'withCustomAttribute'
);

addFilter(
  'editor.BlockListBlock',
  'example/custom-attribute',
  withCustomAttribute
);

最佳答案

我不认为有一种简单的方法可以实现你想要的,因为<BlockListBlock />不支持任何类型的 ref通过。

可以看源码here .

关于reactjs - 在 block 编辑器中获取对 BlockListBlock 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70369563/

相关文章:

wordpress - wp.​​data.select ('core' ).getPostType ('post-type' ) 在古腾堡中返回未定义

javascript - 在react-native TextInput中parseInt()之后值为NaN

javascript - 在 ReactJS 中清除 setInterval()

reactjs - React Native - 在按钮 onPress 中访问 'this' 导致应用程序崩溃

javascript - 在 WordPress Gutenberg block 中调用 DateTimePicker

wordpress-gutenberg - InnerBlocks renderAppender 不做任何事情

reactjs - 在react-router中模块化路由

php - 单击 Ajax WordPress 加载更多自定义帖子

jquery - 如何更改此 carousel-title 类的 CSS 样式?

html - 为什么我的导航栏覆盖了 Fancybox,我该如何解决?