Clicking the button the div (simulates a sheet of paper should change orientation from vertical to horizontal. What's the reason? A state field depending on another field is not correct and due to the async nature, I get this unexpected result? Take into account this is a reduced example of my use case and I need both fields.
import { useState } from "react";
export default function App() {
const [verticalOrientation, setOrientation] = useState(true);
const [dimensions] = useState(getDimensions(verticalOrientation));
const style = {
width: dimensions.width,
height: dimensions.height,
borderStyle: "solid",
};
console.log(verticalOrientation);
return (
<div style={style}>
<h1>Sheet of paper</h1>
<button onClick={() => setOrientation(!verticalOrientation)}>
Change orientation
</button>
</div>
);
}
const getDimensions = (verticalOrientation) => {
const proportion = verticalOrientation ? 16 / 9 : 9 / 16; // A4 vertical vs horizontal
let width = 300;
let height = proportion * width;
console.log(height, proportion, verticalOrientation);
return { width, height };
};