Skip to content

Instantly share code, notes, and snippets.

@rileybathurst
Last active August 19, 2021 18:51
Show Gist options
  • Save rileybathurst/629278026727c9d7a8150df8def9ae3c to your computer and use it in GitHub Desktop.
Save rileybathurst/629278026727c9d7a8150df8def9ae3c to your computer and use it in GitHub Desktop.
Set the height from one element due to another in react
import React, { useState, useEffect, useRef } from 'react';
function Example() {
const [jump, setJump] = useState(0);
useEffect(() => {
const set = useRef();
setJump(set.current.clientHeight);
});
const sectionStyle = {
background: "palegreen",
height: {jump}.jump
}
const twenty = {
height: "20rem"
}
return (
<div>
<div ref={set} style={twenty}>
Set Height
</div>
<section style={sectionStyle}>🦄</section>
</div>
);
}
// markup
const IndexPage = () => {
return (
<main>
<title>Home Page</title>
<Example />
</main>
)
}
export default IndexPage;
@rileybathurst
Copy link
Author

this is built in Gatsby but thats not really the point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment