单击时更改材质 UI IconButton 图标

时间:2021-04-19 21:22:45

标签: reactjs material-ui

我在 IconButton 组件中有一个 DataGrid。如何传递状态以便 onClick 事件更改其中的图标?

    <IconButton size="small" onClick={e => {
changeStateOfIcon();
otherFunction();
}} style={{transform: "rotate(35deg)"}}>
    {this.someState == 'icon1'
    ?
    <Icon1/>
    :
    <Icon2/>
    }
    </IconButton>

1 个答案:

答案 0 :(得分:1)

对于您的图标只有 2 个可能的值,您可以使用布尔状态

const [active,setActive]=useState(false)
<IconButton size="small" onClick={e => {
setActive(!active)
otherFunction();
}} style={{transform: "rotate(35deg)"}}>
    {active ?<Icon1/> : <Icon2/>}
</IconButton>