Skip to content

303: Add the useMediaSrc hook#304

Open
tomjn wants to merge 2 commits intomainfrom
303/use_media_src
Open

303: Add the useMediaSrc hook#304
tomjn wants to merge 2 commits intomainfrom
303/use_media_src

Conversation

@tomjn
Copy link
Copy Markdown
Contributor

@tomjn tomjn commented Nov 9, 2025

Closes #303, adds a quick method of converting an attachment ID into a URL

@tomjn tomjn requested review from Sephsekla and mattheu November 9, 2025 15:44
@tomjn tomjn added the javascript Pull requests that update Javascript code label Nov 9, 2025
Copy link
Copy Markdown
Contributor

@roborourke roborourke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there instances where you'd have just the media ID but not the object? Thinking maybe this could be more useful if it returned the entire media object perhaps, or it could have a second parameter to select a specific property. Then it'd be like useMedia().

@tomjn
Copy link
Copy Markdown
Contributor Author

tomjn commented Nov 10, 2025

Are there instances where you'd have just the media ID but not the object? Thinking maybe this could be more useful if it returned the entire media object perhaps, or it could have a second parameter to select a specific property. Then it'd be like useMedia().

In my local work at the moment I'm using it to fetch the source after picking an image, I could rename this to useMedia though unless you mean there's already a hook with that name?

@roborourke
Copy link
Copy Markdown
Contributor

No that hook doesn't exist to my knowledge, I was just thinking this hook could be more broadly useful, e.g. in case you could fetch dimensions, running time, other media meta data etc...

@tomjn
Copy link
Copy Markdown
Contributor Author

tomjn commented Nov 14, 2025

hmm my concern there is that it would return a media object when the source URL isn't present, but what if it also returned a loading boolean? That way you would know if it's incomplete/loading vs if it doesn't exist or is missing

@roborourke
Copy link
Copy Markdown
Contributor

hmm my concern there is that it would return a media object when the source URL isn't present, but what if it also returned a loading boolean? That way you would know if it's incomplete/loading vs if it doesn't exist or is missing

Not sure I follow, why would it return a media object if source_url isn't present? If I used it for example like useMedia( id, 'source_url' ) we could just write it so that null comes back if the property doesn't exist.

You would get the loading data / boolean I think if you were to just return the value directly from select('core').getMedia() in the hook (which itself is a magic method way of calling getEntityRecord( 'post', 'media', id )), or returned an array that could be destructured. That might be overcomplicating it but if it's easily documentable and has values that are easily distinguished that's good. E.g. usage might be like const [ videoUrl, isVideoLoading ] = useMedia( videoId, 'source_url' );.

@svandragt svandragt removed their request for review November 19, 2025 10:59
@sambulance sambulance removed their request for review December 5, 2025 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript Pull requests that update Javascript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useMediaSrc

2 participants