Created
June 18, 2024 16:47
-
-
Save jm-maniego/bc2dae48cb6b5cb94d8ff2962d3a5305 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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