I have a div with a span and button inside of it. I can toggle the button to hide or show it.
When I do toggle the button, it makes the span jump around, as seen below:
const {
Button,
} = window['material-ui'];
class ButtonContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
hide: false
}
}
handleHide() {
this.setState(previous => ({
hide: !previous.hide
}))
}
render() {
return (
<div style={{"minHeight":"1000px", "padding":"0.05px", "display":"inline-block"}}>
<Button style={{"background":"lightblue", "padding":"10px"}} onClick = {this.handleHide.bind(this)}>{this.state.hide?'show':'hide'} button</Button>
<div>
<span style={{"background":"red", "padding":"10px"}}> jump </span>
{!this.state.hide && <Button style={{"background":"orange"}}> padding </Button>}
</div>
</div>)
}
}
ReactDOM.render(<ButtonContainer/>, document.querySelector("#root"))
<script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@material-ui/core/umd/material-ui.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/babel-standalone@latest/babel.min.js" crossorigin="anonymous"></script>
<div id="root"></div>
I've tried a lot of different things, some of which are from here, including adding display: inline-block
to the parent as well as padding: 0.05px
and it doesn't seem to have the desired effect.
How do I prevnt this jumping when the button is toggled?