如何在 useState 挂钩的帮助下通过单击组件来更改样式组件样式?
我想通过单击来更改 StyledDivGrid 组件的宽度,但是使用此代码,当我单击此 div 时没有任何反应。我无法意识到问题是什么。 请帮忙
const StyledDivGrid = styled.div`
display:flex;
flex-wrap: wrap;
width: ${props=>props.width};
height: 120px;
position: relative;
background-color: lightcoral;
`
function App() {
let [state, setState] = useState(
<StyledDivGrid width = {'120px'}></StyledDivGrid>
)
setState = () => {
<StyledDivGrid width = {'240px'}></StyledDivGrid>;
}
return (
<StyledDivGrid onClick = {() => setState()}>
</StyledDivGrid>
);
}
I want to change width of the StyledDivGrid component by clicking on it, but using this code, when I click on this div nothing happens. I can't realize what the problem is.
Help, pls
const StyledDivGrid = styled.div`
display:flex;
flex-wrap: wrap;
width: ${props=>props.width};
height: 120px;
position: relative;
background-color: lightcoral;
`
function App() {
let [state, setState] = useState(
<StyledDivGrid width = {'120px'}></StyledDivGrid>
)
setState = () => {
<StyledDivGrid width = {'240px'}></StyledDivGrid>;
}
return (
<StyledDivGrid onClick = {() => setState()}>
</StyledDivGrid>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有检查代码,我猜你正在使用 styled-components ,我希望这有用
Didn't check the code and I guess you are using styled-components , i hope this is useful