Spaces:
Configuration error
Configuration error
Download frontend/src/hooks/use-effect-once.ts from flyingdarkdev/README: direct link, hf CLI and curl.
- Browser
- Download file 530 Bytes
-
https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-effect-once.ts
- Command line
-
hf download hf://spaces/flyingdarkdev/README/frontend/src/hooks/use-effect-once.ts
-
curl -L -o use-effect-once.ts https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-effect-once.ts
530 Bytes
| 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]); | |
| }; | |