Skip to content

Commit 280f60f

Browse files
committed
better useEffect
1 parent 418420d commit 280f60f

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

packages/compass-components/src/components/drawer-portal.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,29 @@ export const DrawerAnchor: React.FunctionComponent = ({ children }) => {
278278
Record<string, HTMLButtonElement | undefined>
279279
>({});
280280

281-
useEffect(function () {
282-
const nodes: Record<string, HTMLButtonElement | undefined> = {};
283-
for (const [index, item] of toolbarData.entries()) {
284-
const button = document.querySelector(
285-
`[data-testid="lg-drawer-toolbar-icon_button-${index}"]`
286-
);
287-
nodes[item.id] = button ? (button as HTMLButtonElement) : undefined;
288-
}
289-
setAssistantNodes(nodes);
290-
});
281+
useEffect(
282+
function () {
283+
const nodes: Record<string, HTMLButtonElement | undefined> = {};
284+
for (const [index, item] of toolbarData.entries()) {
285+
const button = document.querySelector(
286+
`[data-testid="lg-drawer-toolbar-icon_button-${index}"]`
287+
);
288+
if (button) {
289+
nodes[item.id] = button as HTMLButtonElement;
290+
}
291+
}
292+
293+
setAssistantNodes((oldNodes) => {
294+
for (const id of Object.keys(nodes)) {
295+
if (nodes[id] !== oldNodes[id]) {
296+
return nodes;
297+
}
298+
}
299+
return oldNodes;
300+
});
301+
},
302+
[toolbarData, assistantNodes]
303+
);
291304

292305
return (
293306
<>

0 commit comments

Comments
 (0)