reactjs - 如何在对话框关闭时重置 Material UI 复选框
问题描述
我通过 Material UI 创建了一个对话框组件,从另一个文件动态导入。
它工作正常,除了此对话框中的复选框(也使用 Material UI 创建)在每次对话框关闭后不会重置。它们仅在页面刷新时重置。其他类型的input
,例如text
或password
自动重置自己。
请注意,该Dialog
组件没有keepMounted = true
,这就是它自动重置其输入的方式。因为,这个值默认为 false。
以下是原始 Dialog 模态组件的代码:
import React, {useState, useEffect} from "react";
import Button from "@material/react-button";
import Divider from "@material-ui/core/Divider";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import TextField from "@material-ui/core/TextField";
import FormGroup from "@material-ui/core/FormGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Checkbox from "@material-ui/core/Checkbox";
const SearchModal = (props) => {
const [checkState, setCheckState] = useState({
checkedQuestions: true,
checkedAnswers: true,
checkedVerified: true,
checkedPending: true,
checkedDisputed: false
});
useEffect(() => {
if(
checkState.checkedQuestions === false &&
checkState.checkedAnswers === false
){
setCheckState({
...checkState,
checkedQuestions: true,
checkedAnswers: true
});
}
if(
checkState.checkedVerified === false &&
checkState.checkedPending === false &&
checkState.checkedDisputed === false
){
setCheckState({
...checkState,
checkedVerified: true,
checkedPending: true,
checkedDisputed: false
});
}
});
const checkSet = (event) => {
setCheckState({
...checkState,
[event.target.name]: event.target.checked
});
}
return(
<Dialog
open={props.searchOpen}
onClose={props.handleClose}
aria-labelledby="searchModalTitle"
aria-describedby="searchModalDescription"
id="searchModal"
>
<DialogTitle id="dialog">{"Search tolodire."}</DialogTitle>
<DialogContent>
<DialogContentText className="marginBottom-17" id="searchModalDescription">
Search for questions or answers.
</DialogContentText>
<TextField
required
type="search"
id="searchQuery"
label="Enter keywords or sentences"
placeholder="Required"
variant="outlined"
data-owner="searchModal"
autoFocus
/>
<DialogContentText className="marginTop-20 marginBottom-10">
Use filters to search in detail.
</DialogContentText>
<FormGroup row className="marginTop-5">
<FormControlLabel
control={
<Checkbox
color="default"
checked={checkState.checkedQuestions}
onChange={(e) => checkSet(e)}
name="checkedQuestions"
/>
}
label="Questions"
/>
<FormControlLabel
control={
<Checkbox
color="default"
checked={checkState.checkedAnswers}
onChange={(e) => checkSet(e)}
name="checkedAnswers"
/>
}
label="Answers"
/>
</FormGroup>
<Divider/>
<FormGroup row>
<FormControlLabel
control={
<Checkbox
color="default"
checked={checkState.checkedVerified}
onChange={(e) => checkSet(e)}
name="checkedVerified"
/>
}
label="Verified"
/>
<FormControlLabel
control={
<Checkbox
color="default"
checked={checkState.checkedPending}
onChange={(e) => checkSet(e)}
name="checkedPending"
/>
}
label="Pending Verification"
/>
<FormControlLabel
control={
<Checkbox
color="default"
checked={checkState.checkedDisputed}
onChange={(e) => checkSet(e)}
name="checkedDisputed"
/>
}
label="Disputed"
/>
</FormGroup>
</DialogContent>
<DialogActions>
<Button raised className="button regularButton font-body" onClick={props.handleClose}>
Search
</Button>
</DialogActions>
</Dialog>
);
}
export default SearchModal
我已经尝试在 Google 和 StackOverflow 上搜索这个问题,但我还没有找到任何解决方案。任何贡献表示赞赏。
PS:handleClose 常量在另一个文件上;
const [searchOpen, setSearchOpen] = useState(false);
const handleSearchOpen = () => {
setSearchOpen(true);
};
const handleClose = () => {
setSearchOpen(false);
};
解决方案
我自己找到了解决方案。
在同一个文件中SearchModal
,我创建了另一个useEffect
字段来检查模式是否打开。当我在预先存在的useEffect
字段中尝试相同时,它创建了无限循环。
对于重置过程,我独立于其他const
s 从头开始分配默认值。
我知道实现可能会更好,有更多的代码重用或动态元素。但是,这只是一个原始的解决方案,直到很快出现更好的解决方案。
所以,解决方案是:
useEffect(() => {
if(props.searchOpen === false){
setCheckState({
checkedQuestions: true,
checkedAnswers: false,
checkedVerified: true,
checkedPending: true,
checkedDisputed: false
});
}
}, [props.searchOpen]);
推荐阅读
- html - 即使正确链接也无法加载资源
- android - 我可以在已发布的 aar 库中包含本地模块吗?
- scala - 在scala中警告或避免整数除法(导致截断)
- php - 如何将另一个 mysqli 查询添加到 while 循环中?
- c# - C#8 什么是“默认!” 对泛型类型做什么?
- ios - 如何解决此警告“'addressDictionary' 在 iOS 11.0 中已弃用:使用 @properties”
- java - 如何从 SprinBoot ResponseEntity 的角度响应中读取 JSONObject 的 KEY 值
- gulp - Gulp:递归地将文件复制到每个子目录
- web2py - auth_user 中额外字段的验证消息不符合可用的翻译。还有其他人有这个问题吗?
- apache-spark - 从正在更新的镶木地板文件中读取错误