I needed bootstrap cards of equal heights inside a react-slick carousel. I found a solution from the following post: React Slick Slide Height Issue
Below is my code, which records the height of the carousel div containing the bootstrap cards. The captured height is then passed to the bootstrap cards as a prop.
const [upcomingEventsHeight, setUpcomingEventsHeight] = React.useState("auto");
React.useEffect(() => {
setTimeout(
() => {
const height = document.getElementsByClassName("slick-track")[0].clientHeight;
setUpcomingEventsHeight(height + "px");
}, 3000);
}, []);
It works fine, except when the browser is resized, because the height is only set when the page is first rendered.
I think that I need to re-render the page when the browser is resized, and I know that I can use
window.addEventListener('resize', ...);
However, I get the error with the following code because React.useEffect cannot be called inside a callback.
window.addEventListener('resize', () => {
React.useEffect(() => {
setTimeout(
() => {
const height = document.getElementsByClassName("slick-track")[0].clientHeight;
setUpcomingEventsHeight(height + "px");
}, 3000);
}, []);
});
I'm not sure how to get around this because I want to use useEffect to re-render the page.