我在 IconButton
组件中有一个 DataGrid
。如何传递状态以便 onClick
事件更改其中的图标?
<IconButton size="small" onClick={e => {
changeStateOfIcon();
otherFunction();
}} style={{transform: "rotate(35deg)"}}>
{this.someState == 'icon1'
?
<Icon1/>
:
<Icon2/>
}
</IconButton>
答案 0 :(得分:1)
对于您的图标只有 2 个可能的值,您可以使用布尔状态
const [active,setActive]=useState(false)
<IconButton size="small" onClick={e => {
setActive(!active)
otherFunction();
}} style={{transform: "rotate(35deg)"}}>
{active ?<Icon1/> : <Icon2/>}
</IconButton>