Skip to content

Commit

Permalink
Remove internal state in favor of mediaURL prop
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf committed Jul 4, 2022
1 parent 6ac9793 commit a7a776a
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions 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 { useEffect, useState, useRef } from '@wordpress/element';
import { useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import {
Expand Down Expand Up @@ -55,18 +55,6 @@ const MediaReplaceFlow = ( {
addToGallery,
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 Expand Up @@ -99,7 +87,6 @@ const MediaReplaceFlow = ( {
onToggleFeaturedImage();
}
closeMenu();
setMediaURLValue( media?.url );
// Calling `onSelect` after the state update since it might unmount the component.
onSelect( media );
speak( __( 'The media file has been replaced' ) );
Expand Down Expand Up @@ -224,14 +211,13 @@ const MediaReplaceFlow = ( {
{ __( 'Current media URL:' ) }
</span>

<Tooltip text={ mediaURLValue } position="bottom">
<Tooltip text={ mediaURL } position="bottom">
<div>
<LinkControl
value={ { url: mediaURLValue } }
value={ { url: mediaURL } }
settings={ [] }
showSuggestions={ false }
onChange={ ( { url } ) => {
setMediaURLValue( url );
onSelectURL( url );
editMediaButtonRef.current.focus();
} }
Expand Down

0 comments on commit a7a776a

Please sign in to comment.