Formik 值未定义
Formik value is undefined
为什么每次改字段都获取不到数据?有人可以帮我配置这个吗?我在我的代码中遗漏了什么吗?我只是希望如果用户单击提交,它将捕获我在 TextField 和 Select
中输入的内容
const [ author, setAuthor] = useState('');
const [ datef, setDateF] = useState('');
const [ datet, setDateT] = useState('');
const handleChangeAuthor = (event) => {
console.log("handleChangeAuthor: ", event)
setAuthor(name)
};
const handleChangesetDateF = (event) => {
console.log("handleChangesetDateF: ", event)
setDateF(name)
};
const handleChangesetDateT = (event) => {
console.log("handleChangesetDateT: ", event)
setDateT(name)
};
const handleSubmit= async(valuesToSubmit) =>
{
console.log("valuesToSubmit: ", valuesToSubmit)
}
............
<Formik
initialValues={{
strdatefrom:datef,
strdateto:datet,
intuserauthorid:author,
intfmdocumentclassificationsid:selecteddocClassification,
intlimit:"100",
intoffset:"0"
}}
onSubmit={(values) =>{ console.log("submit"); handleSubmit(values)}}
>
{({ handleChange, handleBlur, handleSubmit, values, errors, isValid,touched,setFieldValue }) => (
<>
<Grid container>
<Grid xs={12} className={classes.grid}>
<TextField
id="date"
label="Date from"
type="date"
name="datet"
onChange={handleChangesetDateF(value.datet)}
defaultValue="2017-05-24"
name="datef"
className={classes.textField}/>
</Grid>
<Grid xs={12} className={classes.grid}>
<TextField
id="date"
label="Date to"
type="date"
name="datef"
onChange={handleChangesetDateT(value.datef)}
defaultValue="2017-05-24"
className={classes.textField}/>
</Grid>
<Grid xs={12} className={classes.grid}>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel htmlFor="outlined-age-native-simple">Author</InputLabel>
<Select
name="author"
value={author}
onChange={handleChangeAuthor(value.author)}
label="Author">
{users}
</Select>
</FormControl>
</Grid>
</>
)}
</Formik>
from this line please ignore this message, I do this message so that i
can submit a question here
您不必为 formik 使用本地状态,formik 可以在内部管理状态,因此您必须执行类似这样的操作来更新值。
<TextField
id="date"
label="Date from"
type="date"
name="datet"
onChange={event=>setFieldValue('datet',event.target.value)}
defaultValue="2017-05-24"
name="datef"
className={classes.textField}
value={values.datet}/>
formik 中的 setFieldValue 可以更新您可以在 handlesubmit 调用中检索的 formik 状态
为什么每次改字段都获取不到数据?有人可以帮我配置这个吗?我在我的代码中遗漏了什么吗?我只是希望如果用户单击提交,它将捕获我在 TextField 和 Select
中输入的内容 const [ author, setAuthor] = useState('');
const [ datef, setDateF] = useState('');
const [ datet, setDateT] = useState('');
const handleChangeAuthor = (event) => {
console.log("handleChangeAuthor: ", event)
setAuthor(name)
};
const handleChangesetDateF = (event) => {
console.log("handleChangesetDateF: ", event)
setDateF(name)
};
const handleChangesetDateT = (event) => {
console.log("handleChangesetDateT: ", event)
setDateT(name)
};
const handleSubmit= async(valuesToSubmit) =>
{
console.log("valuesToSubmit: ", valuesToSubmit)
}
............
<Formik
initialValues={{
strdatefrom:datef,
strdateto:datet,
intuserauthorid:author,
intfmdocumentclassificationsid:selecteddocClassification,
intlimit:"100",
intoffset:"0"
}}
onSubmit={(values) =>{ console.log("submit"); handleSubmit(values)}}
>
{({ handleChange, handleBlur, handleSubmit, values, errors, isValid,touched,setFieldValue }) => (
<>
<Grid container>
<Grid xs={12} className={classes.grid}>
<TextField
id="date"
label="Date from"
type="date"
name="datet"
onChange={handleChangesetDateF(value.datet)}
defaultValue="2017-05-24"
name="datef"
className={classes.textField}/>
</Grid>
<Grid xs={12} className={classes.grid}>
<TextField
id="date"
label="Date to"
type="date"
name="datef"
onChange={handleChangesetDateT(value.datef)}
defaultValue="2017-05-24"
className={classes.textField}/>
</Grid>
<Grid xs={12} className={classes.grid}>
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel htmlFor="outlined-age-native-simple">Author</InputLabel>
<Select
name="author"
value={author}
onChange={handleChangeAuthor(value.author)}
label="Author">
{users}
</Select>
</FormControl>
</Grid>
</>
)}
</Formik>
from this line please ignore this message, I do this message so that i can submit a question here
您不必为 formik 使用本地状态,formik 可以在内部管理状态,因此您必须执行类似这样的操作来更新值。
<TextField
id="date"
label="Date from"
type="date"
name="datet"
onChange={event=>setFieldValue('datet',event.target.value)}
defaultValue="2017-05-24"
name="datef"
className={classes.textField}
value={values.datet}/>
formik 中的 setFieldValue 可以更新您可以在 handlesubmit 调用中检索的 formik 状态