Skip to content

Instantly share code, notes, and snippets.

@jm-maniego
Created June 18, 2024 16:47
Show Gist options
  • Save jm-maniego/bc2dae48cb6b5cb94d8ff2962d3a5305 to your computer and use it in GitHub Desktop.
Save jm-maniego/bc2dae48cb6b5cb94d8ff2962d3a5305 to your computer and use it in GitHub Desktop.
const LazyCollapse = ({ as: As = 'div', children, deps = [], ...props }) => {
const [show, setShow] = useState(false);
const [timestamp, setTimestamp] = useState(null);
const handleEnter = () => {
setShow(true);
};
const handleExited = () => {
setShow(false);
};
useEffect(() => {
if (props.in) {
setTimestamp(Date.now());
}
}, [props.in].concat(deps));
return (
<Collapse {...props} unmountOnExit mountOnEnter onEnter={handleEnter} onExited={handleExited}>
<As>
{useMemo(() => {
return show ? children() : null;
}, [show, timestamp])}
</As>
</Collapse>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment