【发布时间】:2020-01-05 17:03:41
【问题描述】:
我在渲染时为三个图像图标分配了一种随机颜色。但是,当我单击按钮以呈现对话框弹出窗口时,图标的颜色会再次重新呈现。如何防止这种情况发生?
const colours = [blue[800], green[800], lime[500], brown[500],
grey[500], yellow[800], blueGrey[500],
orange[800], purple[800], red[800], pink[800], indigo[800], cyan[500], teal[500],];
const colour = () => colours[Math.floor(Math.random() * colours.length)];
handleClick= () => {
this.setState({
openDialog: true
});
}
render() {
return (
<div>
<Button onClick={() => this.handleClick()} variant="contained" >
Post
</Button>
<Avatar style={{backgroundColor: colour()}}>S</Avatar>
<Avatar style={{backgroundColor: colour()}}>S</Avatar>
<Avatar style={{backgroundColor: colour()}}>S</Avatar>
<Dialog
keepMounted
fullWidth = "true"
onClose={this.handleClose}
aria-labelledby="customized-dialog-title"
open={this.state.openDialog}
>
</Dialog>
</div>
);
}
【问题讨论】:
-
您可以使用 PureComponent 来防止重新渲染组件。此外,您可以将 react 钩子与单独的 UseState 用于颜色和
openDialog状态。
标签: javascript css reactjs colors