使用UseFormik时如何更改数组的初始值?

发布于 2025-02-13 14:18:44 字数 413 浏览 0 评论 0 原文

这是我使用useformik钩的代码

const formik = useFormik({
  initialValues: {
    name: '',
    status: 'new',
    functionalBreakdown: [],
    spatialBreakdown: [],
    form: [],
    discipline: [],
    companyId,
  },
  validate,
  onSubmit: async (values) => {
    try {
      // const res = await
      setFormData(values);
    } catch (error) {}
  },
});

如何更新数组?如果有人知道解决方案,请提供帮助。

Here is the code where I am using useFormik hook

const formik = useFormik({
  initialValues: {
    name: '',
    status: 'new',
    functionalBreakdown: [],
    spatialBreakdown: [],
    form: [],
    discipline: [],
    companyId,
  },
  validate,
  onSubmit: async (values) => {
    try {
      // const res = await
      setFormData(values);
    } catch (error) {}
  },
});

How can I update the array? Please help if anyone knows the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2025-02-20 14:18:44

如果您要手动更新一个formik值,例如,

formik.setFieldValue(name, "John Doe");

如果您有列表值,则可能会使用此sandbox中的多项选择:

注意:如果要在首次初始化时更新 initialValues ,例如通过更改道具或状态,您需要通过 enableReInitialize:true useformik钩。

In case you want to manually update a formik value you would use e.g.

formik.setFieldValue(name, "John Doe");

If you have list values you would probably use a multi select like in this sandbox:

Edit formik & react-select multi (Typescript) (forked)

Note: If you want to update your initialValues upon first initialization for example through changing props or states you need to pass enableReinitialize: true to useFormik hook.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文