Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
In a REACT Gutenberg block, when a user updates a video element's src attribute how can I make this change visible to the user immediately?

I’m creating a custom Gutenberg block for WordPress using @wordpress/create-block, I'm using the MediaUpload React component, which can be referenced here https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/media-upload/README.md.

The block contains a user updatable HTML5 video element. My question is - how can I make it so when the user uploads the video of choice they see this in the preview of the post edit screen in WordPress? Currently when the user uploads their video and the src attribute is updated they don't see the video in the preview unless they publish the post and then refresh the page.

video element in my code

I’m using the MediaUpload component imported from @wordpress/block-editor When the user selects their image it actually creates an image object, this image object has a series of properties including "url" so we can pass it to a function as an argument.

The media upload component in my code

This updates the src using a simple function. I'm passing the imageObject in and then updating the url using imageObject.url

enter image description here

For clarity, this is how the video element looks in the browser inspector before the user uploads their video.

enter image description here

They then use the upload button in the side panel and this directs them to the media manager to select their file.

enter image description here

When this step is complete and they return to the post edit screen the src attribute of the video element has been updated, but the DOM hasn't refreshed so the user doesn't see the change in the post edit area of Gutenberg. If they publish or save changes and then refresh the page, they will then see the video they added, but this is confusing for the user, I need them to be able to see the change immediately without saving or refreshing. Is there any way I can achieve this?

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not actually setting the "new" image after it has been saved to the variable. Try something like this:

  <MediaUpload
                onSelect={(image) => {
                    setAttributes({ image }) // when selected, save the image in the variable
                }}
                value={image ? image : ''} // if there is a saved image, this will show (so also after selecting one)
                render={({ open }) => (
                    <div>
                        <a
                            href="#"
                            onClick={exampleFunction}
                            title={__('select Image', 'example')}
                            className="image-upload-link" >
                        </a>
                    </div>
                )}
            />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!