Skip to content

Commit

Permalink
Fix useForcedLayout to re-select inner blocks after we manually ins…
Browse files Browse the repository at this point in the history
…ert one (woocommerce#6676)

* Track most recent inner blocks in useForcedLayout

* Force useSelect to re-select when we know innerBlocks has changed

* Rename mapSelect back to select

* Add forcedBlocksInserted to callback deps
  • Loading branch information
opr authored Oct 7, 2022
1 parent d9152c0 commit 1564de2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions assets/js/blocks/cart-checkout-shared/use-forced-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useRef,
useCallback,
useMemo,
useState,
} from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import {
Expand Down Expand Up @@ -41,6 +42,8 @@ export const useForcedLayout = ( {
} ): void => {
const currentRegisteredBlocks = useRef( registeredBlocks );
const currentDefaultTemplate = useRef( defaultTemplate );
const [ forcedBlocksInserted, setForcedBlocksInserted ] =
useState< number >( 0 );

const { insertBlock, replaceInnerBlocks } =
useDispatch( 'core/block-editor' );
Expand All @@ -55,15 +58,16 @@ export const useForcedLayout = ( {
),
};
},
[ clientId, currentRegisteredBlocks.current ]
[ clientId, currentRegisteredBlocks.current, forcedBlocksInserted ]
);

const appendBlock = useCallback(
( block, position ) => {
const newBlock = createBlock( block.name );
insertBlock( newBlock, position, clientId, false );
setForcedBlocksInserted( forcedBlocksInserted + 1 );
},
[ clientId, insertBlock ]
[ clientId, insertBlock, forcedBlocksInserted ]
);

const lockedBlockTypes = useMemo(
Expand Down

0 comments on commit 1564de2

Please sign in to comment.