Cancel previous timeout if flow and reactFlowInstance changes
This commit is contained in:
parent
d19fdf5373
commit
cfec6f2683
1 changed files with 16 additions and 3 deletions
|
|
@ -166,15 +166,28 @@ export default function Page({
|
||||||
|
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
//update flow when tabs change
|
const timeoutRef = useRef<NodeJS.Timeout>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setNodes(flow?.data?.nodes ?? []);
|
setNodes(flow?.data?.nodes ?? []);
|
||||||
setEdges(flow?.data?.edges ?? []);
|
setEdges(flow?.data?.edges ?? []);
|
||||||
setViewport(flow?.data?.viewport ?? { zoom: 1, x: 0, y: 0 });
|
setViewport(flow?.data?.viewport ?? { zoom: 1, x: 0, y: 0 });
|
||||||
setTimeout(() => {
|
|
||||||
|
// Clear the previous timeout
|
||||||
|
if (timeoutRef.current) {
|
||||||
|
clearTimeout(timeoutRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new timeout
|
||||||
|
timeoutRef.current = setTimeout(() => {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}, 300); // Timeout to prevent ReactFlow to appear before viewport is set. Can't make it async because setViewport is not an async function.
|
}, 300);
|
||||||
|
|
||||||
|
// Clear the timeout when the component is unmounted
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timeoutRef.current);
|
||||||
|
};
|
||||||
}, [flow, reactFlowInstance]);
|
}, [flow, reactFlowInstance]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue