mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(ui): optimized useMouseOverNode
Manually hook into pubsub to eliminate extraneous rerenders on hook change
This commit is contained in:
parent
6209fef63d
commit
20f497054f
@ -1,16 +1,17 @@
|
||||
import { useStore } from '@nanostores/react';
|
||||
import { atom } from 'nanostores';
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
export const $mouseOverNode = atom<string | null>(null);
|
||||
|
||||
export const useMouseOverNode = (nodeId: string) => {
|
||||
const mouseOverNode = useStore($mouseOverNode);
|
||||
const [isMouseOverNode, setIsMouseOverNode] = useState(false);
|
||||
|
||||
const isMouseOverNode = useMemo(
|
||||
() => mouseOverNode === nodeId,
|
||||
[mouseOverNode, nodeId]
|
||||
);
|
||||
useEffect(() => {
|
||||
const unsubscribe = $mouseOverNode.subscribe((v) => {
|
||||
setIsMouseOverNode(v === nodeId);
|
||||
});
|
||||
return unsubscribe;
|
||||
}, [isMouseOverNode, nodeId]);
|
||||
|
||||
const handleMouseOver = useCallback(() => {
|
||||
$mouseOverNode.set(nodeId);
|
||||
|
Loading…
Reference in New Issue
Block a user