Reactjs 在react中单击按钮时如何关闭卡?
我有一个材料用户界面卡,其中包含图像、输入字段、复选框和提交按钮。在下面的代码中没有提到的其他选项上,卡显示onclick。我想在单击提交时关闭一张卡。我怎样才能做到这一点Reactjs 在react中单击按钮时如何关闭卡?,reactjs,material-ui,Reactjs,Material Ui,我有一个材料用户界面卡,其中包含图像、输入字段、复选框和提交按钮。在下面的代码中没有提到的其他选项上,卡显示onclick。我想在单击提交时关闭一张卡。我怎样才能做到这一点 <Card className="details-card" style={{ paddingTop: "0px" }} color="primary" > <CardHeader
<Card
className="details-card"
style={{ paddingTop: "0px" }}
color="primary"
>
<CardHeader
style={{
paddingBottom: 0,
paddingTop: 0
}}
title="Image"
/>
<img src="https://unsplash.it/200/?random" />
<CardContent className="details-card-body">
<TextField label="Name" fullWidth />
<Grid container>
<Grid item xs={4}>
<Typography>
New User
<Checkbox
checked={this.state.addNew}
name="addNew"
onChange={this.handleCheckBox("addNew")}
value="new user"
inputProps={{ "aria-label": "Checkbox B" }}
/>
</Typography>
</Grid>
</Grid>
<Button variant="contained" color="primary">
Click to Tag
</Button>
</CardContent>
</Card>
新用户
单击以标记
下面是我在CodeSandbox上的代码
有多种方法可以实现您的目标 您需要一个标志来有条件地隐藏或显示卡。 例如,让我们在状态中使用
flag
变量,并基于提交按钮和您可以执行的此.state.flag
更改状态变量flag
{this.state.flag ?
(<Card
className="details-card"
style={{ paddingTop: "0px" }}
color="primary"
>
//Card content
</Card>)
:
null
}
注意:不推荐第二种方法,因为我们正在渲染元素,即使它不是必需的。有多种方法可以实现您想要做的事情 您需要一个标志来有条件地隐藏或显示卡。 例如,让我们在状态中使用
flag
变量,并基于提交按钮和您可以执行的此.state.flag
更改状态变量flag
{this.state.flag ?
(<Card
className="details-card"
style={{ paddingTop: "0px" }}
color="primary"
>
//Card content
</Card>)
:
null
}
注意:不推荐第二种方法,因为我们正在渲染元素,即使它不是必需的