I'm trying to get the featured image of selected post, but I'm getting undefined when I try this:
const media = select('core').getMedia( FEATURED_IMAGE_ID );
console.log(media);
It turns to undefined when it's first try. If trigger the function for second time, it gives the json. I tried the async function but no luck.
Anyone have any ideas?
As per a similar issue in the
WordPress/gutenbergrepository:This was posted on June 6th 2019 and thus, a more succinct solution exists as per this WordPress developer blog article:
Thus, you could use
useSelectas follows in your block:This doesn't get around the root predicament that
getMediacan beundefined, but this does seem to be the recommended way to handle the result. You should perhaps consider an empty state and/or a loading state in your block.