[英]How to Set The Value in Textfield in Formik?
How to Set The Value In TextField with Conditon in Formik?如何在 Formik 中使用 Conditon 设置 TextField 中的值? Basically I want to select the name and then this name is automatically set to the TextField but I cant find the proper way to do it, Can Anybody Please Help:!!基本上我想要 select 这个名字,然后这个名字自动设置为 TextField 但我找不到正确的方法,任何人都可以帮助:!! Thanks In Advance :)))))))))))))提前致谢 :)))))))))))))
This is TextField Component这是 TextField 组件
import React from 'react';
import TextField from '@mui/material/TextField'
import { useField } from 'formik';
const TextfieldWrapper = ({
name,
...otherProps
}) => {
const [field, mata, helpers] = useField(name);
const configTextfield = {
...field,
...otherProps,
fullWidth: true,
variant: 'outlined',
margin: "normal",
size: 'small'
};
if (mata && mata.touched && mata.error) {
configTextfield.error = true;
configTextfield.helperText = mata.error;
}
return (
<TextField
sx={{
"& .MuiInputBase-root": {
height: 45
}
}}
{...configTextfield}
/>
);
};
export default TextfieldWrapper;
and This is Dashboard Component这是仪表板组件
<Formik
enableReinitialize={true}
initialValues={{ ...initialFormState }}
validationSchema={formValidation}
onSubmit={(values, { resetForm }) => {
console.log(values);
valueSheet.postSheet();
setShow(false);
resetForm({ values: '' });
setTimeout(() => {
window.location.reload(false);
}, 2000);
}}
>
<Form>
<div>
<TextfieldWrapper
name='sheetNo'
inputRef={valueSheet.sheetName}
label="New Sheet Name"
/>
</div>
</Form>
I have to confess I am not so sure what you wanna achieve.我不得不承认我不太确定你想要达到什么。 But useField
hook gets a string or a configuration object as its argument.但是useField
钩子获取一个字符串或一个配置 object 作为它的参数。 You can use it to set its initial value.您可以使用它来设置其初始值。
const [field, meta, helpers] = useField({
name,
value,
});
Now you can pass the value down to the component.现在您可以将值向下传递给组件。
<TextfieldWrapper
name='sheetNo'
inputRef={valueSheet.sheetName}
label="New Sheet Name"
value={value}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.