admin管理员组文章数量:1122846
I want to get the selected block (an inner block) every time a new block is selected.
I am trying the following:
edit() {
const selectedBlock = wp.data.select( 'core/block-editor' ).getSelectedBlock();
useEffect( () => {
console.log( selectedBlock );
}, [ selectedBlock ] );
})
The selected block is returned only when the block renders. Nothing happens when I select a new inner block. It works only when I click out of the parent block, then click again an inner block.
I want to get the selected block (an inner block) every time a new block is selected.
I am trying the following:
edit() {
const selectedBlock = wp.data.select( 'core/block-editor' ).getSelectedBlock();
useEffect( () => {
console.log( selectedBlock );
}, [ selectedBlock ] );
})
The selected block is returned only when the block renders. Nothing happens when I select a new inner block. It works only when I click out of the parent block, then click again an inner block.
Share Improve this question asked Jul 19, 2022 at 22:15 CyberJCyberJ 4375 silver badges19 bronze badges 1- Does this answer your question? How do I access site and block editor state data and use `useSelect()` or `withSelect()` to bind it to my components? – bosco Commented Jul 20, 2022 at 5:33
1 Answer
Reset to default 0In short, use the useSelect()
hook or the withSelect()
HOC utility from @wordpress/data
to create a subscription to selected value. Either will trigger a render whenever the selected value changes in state:
import { useSelect } from '@wordpress/data';
edit() {
const selectedBlock = useSelect(
( select ) => select( 'core/block-editor' ).getSelectedBlock()
);
useEffect( () => {
console.log( selectedBlock );
}, [ selectedBlock ] );
}
本文标签: Get selected block every time a new block is selected in Gutenberg
版权声明:本文标题:Get selected block every time a new block is selected in Gutenberg 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284667a1927298.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论