How can I detect when a featured image changes in Gutenberg?

Viewed 19

I am trying to detect when the user changes or deletes the featured image on the edit/add screen and do something accordingly. As I am a new in react, I am lost a little bit. I saw posts telling to use useSelect() for that but I can't get it to work. Here is what I have so far

const AWP_Example_Plugin = () => {
        const media = useSelect( ( select ) => {        
            const { getMedia } = select( 'core' );
            const { getEditedPostAttribute } = select( 'core/editor' );
            const featuredImageId = getEditedPostAttribute( 'featured_media' );
    
            return {
                media: featuredImageId ? getMedia( featuredImageId ) : null,
                featuredImageId,
            };
        } );
        
        console.log("Featured image = ", media);
        return (<div></div>);
    }

But I'm getting a syntax error on the last return statment. Am I on the right track?

0 Answers
Related