【发布时间】:2021-05-23 06:09:00
【问题描述】:
如何用按钮改变div的宽度和高度? 我是 React 新手,我几天前开始学习,所以请理解 我有这个:
const MyRectange = styled.div`
width:${props => props.width+'px'},
height:${props => props.height+'px'},
backgroundColor: "#000",
display: 'flex',
textAlign: 'center',
alignItems: 'center',
justifyContent: 'center',
transition: 'all 0.5s'
`;
let [width, setWidth] = useState(100);
let [height, setHeight] = useState(150);
const max = 500;
const min = 100;
const Draw = () => {
width = Math.floor(Math.random() * (max - min)) + min;
height = Math.floor(Math.random() * (max - min)) + min;
setWidth = width;
setHeight = height;
}
return(
<div>
<MyRectange
width = {width}
height = {height}
>
<p>Width: {width} px</p>
<p>Height: {height} px</p>
<button onClick = {() => Draw()}>Draw</button>
</div>
);
每次点击按钮尺寸都应更改为随机数后它不起作用,我不知道如何解决
【问题讨论】:
标签: reactjs button react-hooks