简体   繁体   中英

How to set formik values

index.js

const [request, setRequest] = useState({
    product_name: '',
    categoryId: '',
    description: '',
    link: ''
});

const loadRequest = async () => {
    const res = await Axios.get(`http://localhost:3001/dashboard/${id}`, {
        headers: headers
    });
    let _request = res.data.response
    setRequest({
        product_name: _request.product_name,
        categoryId: _request.categoryId,
        description: _request.description,
        link: _request.link
    })
};

const validationSchema = Yup.object({
    product_name: Yup.string().trim().required('Product name is required'),
    description: Yup.string().trim().required('Description is required'),
    category: Yup.string().required('Category is required')
});

const formik = useFormik({
    initialValues: request,
    onSubmit: handleSubmit,
    validationSchema: validationSchema
});
console.log(formik.values)  =====>>> Here I am not getting data
console.log(request)  ==========>>> Here I am getting data

I am getting values in the request state but when I set values in initial values in formik it is not showing data. Can someone tell me what am I missing here?

刚刚添加 enableReinitialize: true 并且有效

//you can add the reset form in your onSubmit. This works for me. //eg onSubmit: (values, { resetForm }) => { resetForm({ values: "" }); }, onSubmit: (values, { resetForm }) => { resetForm({ values: "" }); },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM