【发布时间】:2021-03-29 07:15:51
【问题描述】:
代码返回错误 TypeError: Cannot read property 'trim' of undefined,
const [category, setCategory] = useState();
const classes = useStyles();
const [fields, setFields] = useState([''
]);
const onChangeHandler = (e, i) => {
const { name , value } = e.target
const currentFields = [...fields];
currentFields[i] = value;
setFields(currentFields)
}
//Validators
const [errors, setErrors] = useState ('');
const onSubmit = (e) => {
e.preventDefault()
let errors = {};
if (!category.trim()){
errors.category="Select Type of Category is Required"
}
if (!fields.trim()){
errors.fields="Field is Required/Remove empty Fields"
}
setErrors(errors)
if (Object.keys(errors).length === 0){
formSubmit()
}
}
【问题讨论】:
标签: reactjs validation