-
-
Notifications
You must be signed in to change notification settings - Fork 49k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Form.useWatch doesn't return initial values #49010
Comments
Your codesandbox is private. Please help set it to public~ |
Hello @alex-golubtsov. Please provide a online reproduction by forking codesandbox of antd@5.x or antd@4.x, or provide a minimal GitHub repository. Issues labeled by 你好 @alex-golubtsov,我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击这里创建一个 antd@5.x 或 antd@4.x 的 codesandbox,或者提供一个最小化的 GitHub 仓库。3 天内未跟进此 issue 将会被自动关闭。 |
@zombieJ right, I'm sorry, just made public |
The first return of |
|
What if I clear it manually? For example, |
you can 😁 const name = useWatch('name')
const nameHasDefaultValue = name ?? 'zhangsan'
|
😆😆😆 const App: React.FC = () => {
const [form] = Form.useForm();
const name = Form.useWatch("name", form);
const nameHasDefaultValue = name ?? "John";
return (
<Form form={form} initialValues={{ name: "John" }}>
<Form.Item label="Name" name="name">
<Select
options={[
{
label: "😄",
value: "John",
},
{
label: "😭",
value: "Roy",
},
]}
allowClear
/>
</Form.Item>
value: {nameHasDefaultValue}
</Form>
);
}; Guess what the value of name will become if I click clear? It will become |
As @crazyair said. |
Using this custom hook which returns the form's value if /**
* Wrapper of Form.useWatch to handle initial render race condition.
* Form.useWatch always yields undefined in the first render.
* This wrapper handles that by sending the value from form.getFieldValue if the watched value is undefined.
* This will get corrected from second render.
* @see https://github.com/ant-design/ant-design/issues/49010#issuecomment-2123979311
* @param {string|string[]} field - Field Name.
* @returns {*} Watched value
*/
export const useWatch = (...args) => {
const form = Form.useFormInstance();
const value = Form.useWatch(...args);
return value !== undefined ? value : form.getFieldValue(args[0);
};
|
I propose that it should register when useForm() is first called, maybe allow passing initialValues in there. I think this behavior is quite confusing, especially for people coming from react-hook-form |
The const [form] = Form. useForm()
const name= Form.useWatch('name',form)
<Form initialValues={{ name: 'zhangsan'}} |
antd能否改成 |
const [form] = Form.useForm<{ name: string }>();
const nameWatch = Form.useWatch('name', form);
const nameValue = form.isFieldTouched('name') ? nameWatch : 'init'; |
Reproduction link
Steps to reproduce
form instance: John watcher: undefined
This means
John
value comes fromuseFormInstance
andundefined
comes fromuseWatch
What is expected?
I'd expect both values to be equivalent
What is actually happening?
useWatch
returnsundefined
value for a field, but in reality it's defined byForm
'sinitialValues
The text was updated successfully, but these errors were encountered: