Check the following component (live example shown in this snippet https://codesandbox.io/s/reverent-mccarthy-r3v93?file=/src/App.tsx):
import * as React from "react";
export default function App() {
const [indexOfDiagnosis, setIndex] = React.useState(0);
return (
<div>
<button onClick={() => setIndex(indexOfDiagnosis + 1)}>click</button>
<div>
{[1, 2, 3, 4, 5, 6, 7].map((_, index) => (
<img
key={index}
alt={"asd"}
src={
index % 2
? "https://images.unsplash.com/photo-1563630381190-77c336ea545a?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=1872&q=80"
: "https://images.unsplash.com/photo-1494548162494-384bba4ab999?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=80"
}
width="80%"
height="80%"
style={{ height: index !== indexOfDiagnosis ? "0px" : undefined }}
/>
))}
</div>
</div>
);
}
It shows a list of images, where all but one have height: 0
(these are implicitly hidden).
But, when you click the button to iterate through them, further images are shown with a top offset (as if the previous images still take some space). Why is this happening?
NOTE 1: In devtools the height, padding, border and margin of each "invisible" image is shown as 0.
NOTE 2:
Also by setting the parent div to display: flex; flex-direction: column
this offset bug does not happen.