Created
April 17, 2025 13:59
-
-
Save OliverJAsh/9475055d2dc4769696d97f5401a0407f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Freeze: FC<{ freeze: boolean; children: ReactNode }> = ({ | |
freeze, | |
children, | |
}) => { | |
const outerRef = useRef<HTMLDivElement>(null); | |
const ref = useRef<HTMLDivElement>(null); | |
useLayoutEffect(() => { | |
if (freeze) { | |
outerRef.current!.appendChild(ref.current!); | |
} | |
}, [freeze]); | |
return freeze ? ( | |
<div ref={outerRef} /> | |
) : ( | |
// Key is needed to ensure React hydrates again. | |
<div key="defrosted" ref={outerRef}> | |
<div | |
ref={(el) => { | |
// Don't clear when it's removed, otherwise it will be gone before the | |
// effect. | |
if (el !== null) { | |
ref.current = el; | |
} | |
}} | |
> | |
{children} | |
</div> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment