Skip to content

Commit

Permalink
MediaReplaceFlow: Keep internal URL state synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Jul 4, 2022
1 parent f751382 commit 6ac9793
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/block-editor/src/components/media-replace-flow/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState, useRef } from '@wordpress/element';
import { useEffect, useState, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import {
Expand Down Expand Up @@ -56,6 +56,17 @@ const MediaReplaceFlow = ( {
handleUpload = true,
} ) => {
const [ mediaURLValue, setMediaURLValue ] = useState( mediaURL );

// In the event of an external change to prop `mediaURL`, synchronize
// internal state `mediaURLValue`. Synchronization is especially important
// when dealing with images that are being uploaded and whose URL changes
// from a blob to a proper URL.
useEffect( () => {
if ( mediaURL && mediaURL !== mediaURLValue ) {
setMediaURLValue( mediaURL );
}
}, [ mediaURL ] );

const mediaUpload = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().mediaUpload;
}, [] );
Expand Down

0 comments on commit 6ac9793

Please sign in to comment.