gpt4 book ai didi

javascript - react Material 重置表格值

转载 作者:行者123 更新时间:2023-12-02 21:32:05 27 4
gpt4 key购买 nike

我正在使用带钩子(Hook)的 react Material ,我想对 Material 形式进行重置。

我一直在尝试设置状态,但没有反射(reflect)任何变化

<form className="create-account-form" autoComplete="off" onSubmit={onSubmit}>
<FormControl
error={!!errorText.first_name}
variant="outlined"
size="small"
fullWidth
>
<InputLabel>First Name</InputLabel>
<OutlinedInput
name="first_name"
className="create-account-input"
labelWidth={90}
disabled={loading}
onChange={event => handleChange("first_name", event)}
/>
</FormControl>
<Button
type="submit"
className="submit-button"
color="primary"
variant="contained"
>
Submit
</Button>
</form>;
//my state varibles

const [signUpData, setSignUpData] = useState({
first_name: ""
});
const handleChange = (field, event) => {
if (event) {
setSignUpData({
...signUpData,
[field]: event.target ? event.target.value : event
});
}
};

提交时我想清除输入字段中的数据。

const onSubmit = e => {
e.preventDefault();
setSignUpData((prev) => (prev.first_name = "",));

// clearfunction()
};

最佳答案

加载初始状态

const init = {first_name: ''};
const [signUpData, setSignUpData] = useState(init)

const onSubmit = (e) => {
e.preventDefault();
// do submit logic
setSignUpData(init)
}

关于javascript - react Material 重置表格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60597714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com