-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reinstate state “reset” but avoid passing internalInputValue as dep t…
…o effect
- Loading branch information
Showing
1 changed file
with
12 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 12 additions & 1 deletion
13
packages/block-editor/src/components/link-control/use-internal-input-value.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { useState, useEffect } from '@wordpress/element'; | ||
|
||
export default function useInternalInputValue( value ) { | ||
const [ internalInputValue, setInternalInputValue ] = useState( | ||
value || '' | ||
); | ||
|
||
// If the value prop changes, update the internal state. | ||
useEffect( () => { | ||
setInternalInputValue( ( prevValue ) => { | ||
if ( value && value !== prevValue ) { | ||
return value; | ||
} | ||
|
||
return prevValue; | ||
} ); | ||
}, [ value ] ); | ||
|
||
return [ internalInputValue, setInternalInputValue ]; | ||
} |