蚂蚁设计形式之间的区别

发布于 2025-01-28 09:02:06 字数 535 浏览 3 评论 0原文

如标题所述。两者有什么区别?

1。

import { Form } from 'antd';

const Components = () => {
  const [form] = Form.useForm();
  form.setFieldsValue({ ... });

  return (
    <Form form={form}>
      ...
    </Form>
  );
}
import { Form } from 'antd';

const Components = () => {
  return (
    <Form initialValues={{ ... }}>
      ...
    </Form>
  );
}

As the title states. What is the difference between the two?

1.

import { Form } from 'antd';

const Components = () => {
  const [form] = Form.useForm();
  form.setFieldsValue({ ... });

  return (
    <Form form={form}>
      ...
    </Form>
  );
}
import { Form } from 'antd';

const Components = () => {
  return (
    <Form initialValues={{ ... }}>
      ...
    </Form>
  );
}

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

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

发布评论

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

评论(1

瑾兮 2025-02-04 09:02:06

setFieldValues 非常适合按需设置值。例如,如果您的表单上有一个“默认”按钮,将所有值设置为默认值。在表单上使用setFieldValues的问题是,如果您将道具传递给该更改或功能道具,则当props刷新应用程序中时,SetFieldValues将一遍又一遍地调用。这可能会导致表单在用户具有表单上方时随机重置为setFieldValues中的值。

initialValues 仅在形式初始化并且没有与上述相同的问题时设置初始值,因此,它的理想是设置具有初始值的形式,同时具有可能令人耳目一新或更改的道具。这会导致您的表格表现良好,并且在销毁表单之前不会改变用户。

setFieldValues is good for on-demand setting of values. For example, if there was a "Default" button on your form that sets all the values back to some default. The problem with using setFieldValues on form, is that if you are passing props down that change or a function prop, the setFieldValues will be called over and over again when props refresh in the app. This can cause the form to reset to the values in setFieldValues randomly while the user has the form up.

initialValues only sets the initial values when the form initializes and does not have the same issue as stated above, so its ideal for setting up a form with initial values while having props that may be refreshing or changing. This causes your form to behave well and not change on the user until the form is destroyed.

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