我正在使用material-ui的IconButton
,并且我想在单击/触摸事件后更改按钮的图标。
var tableModeElement =
<IconButton key="tableModeButton"
onTouchTap={() => {
this.setState(prevState => (
{ isCardView: !prevState.isCardView })) } }>
<i className="material-icons theme-color-p1">
{this.state.isCardView ? "view_module" : "list"}
</i>
</IconButton>
表达式
{this.state.isCardView ? "view_module" : "list"}
只被计算一次,之后不再计算。我原以为如果我改变state,就可以强制重新渲染,那我做错了什么呢?编辑:添加了IconButton被分配给一个变量的信息。如果将
<IconButton>
直接包含在render方法中,它就可以正常工作。我必须重新分配变量才能使它正常工作。