How do I add an if statement in a return statement of this sort:
return {
ID: select('core/editor').getEditedPostAttribute('featured_media'),
if (ID) {
media: select('core').getMedia( ID )
}
};
Clearly, I can’t just drop an if statement if () directly within return {}.
>Solution :
There are 2 things you can do, either use a ternary operator inline:
return {
ID: select('core/editor').getEditedPostAttribute('featured_media'),
media: (ID ? select('core').getMedia( ID ) : undefined)
};
or create the object and conditionally add a new property
const obj = {
ID: select('core/editor').getEditedPostAttribute('featured_media')
};
if(ID){
obj.media = select('core').getMedia( ID )
}
return obj