Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block editor: avoid list re-rendering on select #58435

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,42 @@ function Items( {
__experimentalAppenderTagName,
layout = defaultLayout,
} ) {
const selected = useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
const order = getBlockOrder( rootClientId );
const selectedBlocks = getSelectedBlockClientIds();
const visibleBlocks = __unstableGetVisibleBlocks();
let hasVisibleBlocks = false;
let hasSelectedBlocks = false;
for ( const clientId of selectedBlocks ) {
if ( visibleBlocks.has( clientId ) ) {
hasVisibleBlocks = true;
}
if ( selectedBlocks.includes( clientId ) ) {
hasSelectedBlocks = true;
}
}
return {
order,
// Only pass selectedBlocks and visibleBlocks if they are
// relevant to the root block. Otherwise changes in visible
// blocks or selected blocks cause all block lists to re-render.
selectedBlocks: hasSelectedBlocks ? selectedBlocks : null,
visibleBlocks: hasVisibleBlocks ? visibleBlocks : null,
temporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks(),
};
},
[ rootClientId ]
);
const { order, selectedBlocks, visibleBlocks, temporarilyEditingAsBlocks } =
useSelect(
( select ) => {
const {
getBlockOrder,
getSelectedBlockClientIds,
__unstableGetVisibleBlocks,
__unstableGetTemporarilyEditingAsBlocks,
} = select( blockEditorStore );
return {
order: getBlockOrder( rootClientId ),
selectedBlocks: getSelectedBlockClientIds(),
visibleBlocks: __unstableGetVisibleBlocks(),
temporarilyEditingAsBlocks:
__unstableGetTemporarilyEditingAsBlocks(),
};
},
[ rootClientId ]
);
selected;

return (
<LayoutProvider value={ layout }>
Expand All @@ -183,8 +200,8 @@ function Items( {
value={
// Only provide data asynchronously if the block is
// not visible and not selected.
! visibleBlocks.has( clientId ) &&
! selectedBlocks.includes( clientId )
! visibleBlocks?.has( clientId ) &&
! selectedBlocks?.includes( clientId )
}
>
<BlockListBlock
Expand Down
Loading