In 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 ] );
}
Related Posts:
- Add To Gutenberg Sidebar
- How to detect the usage of Gutenberg
- Possible to use @wordpress/create-block with multiple blocks?
- What is the advantage of ‘register_block_type’ (the PHP function) when creating custom blocks?
- The Block Editor: Disable “Color settings” in the specific block
- Serialize custom block with InnerBlock
- What replaces wpColorPicker in Gutenberg?
- Gutenberg Range Control default from meta value
- Find all the places where a block type is used
- Why is the new Gutenberg editor so narrow, and how to make it wider?
- WP Blocks – Gutenberg – not rendering $content
- Why does the custom HTML block not preserve the HTML characters?
- Can’t get dynamic Gutenberg block to render on the frontend
- Gutenberg change/remove “Write your story” placeholder in custom blocks
- How do I listen for a change in an innerBlock child?
- How to enable Gutenberg Block Editor on taxonomy term pages?
- What’s the absolute minimum code that I need to create a dynamic block?
- How to make 2/3 width column in Gutenberg
- Change default colors in paragraph block settings
- Gutenberg Reusable Block of WordPress
- Add custom attributes to link in Gutenberg
- How do I add a Gutenberg editor to a custom options page
- Custom Gutenberg Block: Excluding an allowed block from InnerBlock’s templateLock={‘all’}
- Determine if block has already been registered
- InnerBlocks breaks Flexbox and CSS Grid styles
- Is there a way to set the gutenberg color palette outside the theme?
- Gutenberg get withState variable in save function
- Gutenberg block JavaScript localisation on Polyglots does not work
- how define elements type in attributes type array of gutenberg blocks
- Convert GMT time to local time in Gutenberg Block Editor
- Restrict heading level in Gutenberg block `core/heading`
- How do I disable or hide the Layout block setting in the Group block
- Cleaning up after block dismount
- I cant see Block Editor with v5.7
- How to update block attribute after format is applied?
- How to add a class to the link of the default Gutenberg Button block?
- How to identify what templates are being loaded in a block-based theme [closed]
- Check if current page is using blocks (Gutenberg) or is legacy
- Add support for ‘alignnone’ to Gutenberg block align controls?
- How do I replace a audio gutenberg block to native audio wordpress
- Converting a theme to Gutenberg but some styles are missing in the front end [closed]
- React useEffect not work in frontend gutenberg blocks
- Gutenberg Block language translation does not work
- How to generate block ID for use in post_content JSON data
- Where in a WordPress installation is the code for the default Gutenberg blocks located? [duplicate]
- Display selected image in the MediaPlaceholder component
- ACF and Gutenberg block, how to use blocks without ?
- Submitting a block to the .org repo: do I submit compiled code, or source code? [closed]
- Using 2 HTML blocks in Gutenberg to wrap content with div
- Add button to Block toolbar: toggleFormat is undefined
- What is the name of the user capability to read a reusable block?
- Gutenberg – Prevent Column Nesting in the Visual Editor
- How to remove whitespaces from source of posts created through WordPress Gutenberg editor
- Gutenberg blocks (block editor) with get_the_excerpt and get_the_content, using ACF
- Basic use of useState
- Using full site editing, can I edit just one specific post / page to be unique?
- Detect selected block pattern from a specific Pattern category
- How to add a message when post category is empty (the category have no published posts)?
- Add javascript file to a block pattern
- Block editor not recognizing markdown
- Can’t add front end script file using block.json for a custom block
- How to create Gutenberg patterns and patterns categories based on CPT posts and CPT terms?
- Gutenberg Block: Query for posts with blocks and sort by attributes
- How to Disable “Block Recovery” for HTML Blocks in Gutenberg
- Block error: post_taxs_obj is not a valid property of Object
- How to get the Gutenberg image block to show new Media Library listing
- useSelect in event callback – not retrieving value on time
- How to Batch Convert Gutenberg post to Classic post?
- How to create pre-designed page layouts for Gutenberg?
- Can anyone explain why setting a block attributes source to a data-attribute doesn’t work?
- How to: Provide component with innerBlocks to WordPress Gutenberg’s save function?
- How can I display instagram block with parse_block?
- Do not wrap custom block in an additional DIV but instead use only the JSX I provide
- Line break in Gutenberg post title?
- Basic Block Usage Help on WordPress
- Can’t extend my custom gutenberg block
- Is there an easy way to remove the data-preserver-spaces span from blocks?
- How to Stop Gutenberg From Adding Classic Block
- How to increase width of code block in WordPress blog?
- Video, embed, html block usage out of the_content
- applyformat in gutenberg doesnt work and no errors
- Why does Gutenberg adds 4 around blocks? And how to change this?
- admin_notices not shown
- Have parent block’s isSelected be true if an innerblock is selected?
- How can I add the option to format to list in RichTextToolbarButton?
- Layout Settings in the new Gutenberg editor
- How to get blocks with same heigh in columns?
- Gutenberg: Issue trying to commit locally
- Block UI data table
- How to use correctly Panel in block/editor?
- Block editor – Too many requests for server host
- How to generate WordPress core blocks markup programmatically?
- How to restrict gutenberg block next post & previous post to same category?
- Where is getSite selector defined?
- How properly use social link block in template part
- How to self host fonts and have them show up in all the Full Site Block Editor Typography options, including global styling
- How do I have two RadioControls in my custom Gutenberg block?
- WordPress block editor template parts not saving
- Conditionally Load JS for Block Style
- Custom WP Blocks (register_block_type) in a Parent Theme