Javascript 使用formik时在标签中显示活动状态
我一直在使用表单的Javascript 使用formik时在标签中显示活动状态,javascript,reactjs,formik,Javascript,Reactjs,Formik,我一直在使用表单的redux表单,但我想切换到formik,但我找不到显示我的输入字段是否处于活动状态的方法。就像我以前使用的redux表单 {label && ( <Label isActive={meta !== undefined && meta.active} css={labelCss}> {label} </Label> )} {label&&( {label} )} 但是,如何在formik中做到这一点
redux表单
,但我想切换到formik
,但我找不到显示我的输入字段是否处于活动状态的方法。就像我以前使用的redux表单
{label && (
<Label isActive={meta !== undefined && meta.active} css={labelCss}>
{label}
</Label>
)}
{label&&(
{label}
)}
但是,如何在formik中做到这一点呢
这是我的密码
const TextField = ({ label, type, ...props }) => {
const [field, meta] = useField(props);
const errorText = meta.error && meta.touched ? meta.error : "";
console.log('meta', meta)
return (
<>
<InputGroup>
{label && <Label>{label}</Label>}
<Input type={type} {...field} {...props} />
</InputGroup>
</>
);
};
consttextfield=({label,type,…props})=>{
const[field,meta]=useField(props);
常量errorText=meta.error&&meta.toucted?meta.error:;
console.log('meta',meta)
返回(
{label&&{label}
);
};
我在formik中没有看到任何活动道具 您可以使用输入字段支持的
onFocus
prop
const [active, setActive] = useState(false)
<Input
onBlur={() => setActive(false)}
onFocus={() => setActive(true)}
{...otherProps}
/>
const[active,setActive]=useState(false)
setActive(假)}
onFocus={()=>setActive(true)}
{…其他道具}
/>
它不支持formik。您需要在TextField状态中处理活动状态。只需使用handleFocus
和handleBlur
功能即可更新激活状态。