Skip to content

Commit

Permalink
update the prop prev value properly
Browse files Browse the repository at this point in the history
  • Loading branch information
retrofox committed Apr 11, 2024
1 parent a12fbcf commit b5e7ada
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ const BindingConnector = ( {
const prevAttrValue = useRef( attrValue );
const prevPropValue = useRef(); // `undefined` for the fisrt sync (from source to block).

/*
* Update the bound attribute value,
* casting the value to the original type.
*
* @param {string} next - The new attribute value.
* @param {string} current - The current attribute value.
* @return {void}
*/
const updateBoundAttibute = useCallback(
( next, current ) =>
onPropValueChange( {
Expand All @@ -142,6 +150,7 @@ const BindingConnector = ( {

useLayoutEffect( () => {
const rawAttrValue = getAttributeValue( attrValue );

if ( typeof propValue !== 'undefined' ) {
/*
* On-sync from external property to attribute.
Expand All @@ -150,13 +159,13 @@ const BindingConnector = ( {
* update the attribute value.
*/
if ( propValue !== prevPropValue.current ) {
// Store the current propValue to compare in the next render.
prevPropValue.current = propValue;

if ( propValue !== rawAttrValue ) {
updateBoundAttibute( propValue, attrValue );
return; // close the sync cycle.
}

// Store the current propValue to compare in the next render.
prevPropValue.current = propValue;
}
} else if ( placeholder ) {
/*
Expand Down

0 comments on commit b5e7ada

Please sign in to comment.