mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
19 lines
530 B
TypeScript
19 lines
530 B
TypeScript
import React from "react";
|
|
|
|
// Introduce this custom React hook to run any given effect
|
|
// ONCE. In Strict mode, React will run all useEffect's twice,
|
|
// which will trigger a WebSocket connection and then immediately
|
|
// close it, causing the "closed before could connect" error.
|
|
export const useEffectOnce = (callback: () => void) => {
|
|
const isUsedRef = React.useRef(false);
|
|
|
|
React.useEffect(() => {
|
|
if (isUsedRef.current) {
|
|
return;
|
|
}
|
|
|
|
isUsedRef.current = true;
|
|
callback();
|
|
}, [isUsedRef.current]);
|
|
};
|