Skip to content
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 为 undefined 导致页面报错 #48680

Closed
crazyair opened this issue Apr 28, 2024 · 6 comments
Closed

Form.useWatch 为 undefined 导致页面报错 #48680

crazyair opened this issue Apr 28, 2024 · 6 comments

Comments

@crazyair
Copy link
Member

crazyair commented Apr 28, 2024

Reproduction link

Edit on StackBlitz

Steps to reproduce

打开 demo

What is expected?

不报错

What is actually happening?

报错

Environment Info
antd 5.16.5
React latest
System mac
Browser chrome

name 如果不传,则是 undefined,但是 value 不是 undefined,而是 {},希望当 name 为 undefined、null、false,返回值为 undefined

Copy link

stackblitz bot commented Apr 28, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@Wxh16144
Copy link
Member

image

文档说了第一个参数必填,感觉没必要做太多防御性编程,应该由使用开发者自己判断。

@crazyair
Copy link
Member Author

image 文档说了第一个参数必填,感觉没必要做太多防御性编程,应该由使用开发者自己判断。

类型定义的是必填的,但是其实可以

const ddd: string | undefined = undefined;
    Form.useWatch(ddd);

不报错,这个 useWatch 的类型定义估计有点问题,但

如果是 useWatch('') 估计也不会有类型错误,

另外其实可以优化,如果 useWatch 的 name 参数找不到 value 可以返回空,不返回 {}

@MadCcc
Copy link
Member

MadCcc commented May 8, 2024

JSON.stringify

@MadCcc MadCcc closed this as completed May 8, 2024
@crazyair
Copy link
Member Author

crazyair commented May 8, 2024

应该是 useWatch 返回 undefined

@crazyair
Copy link
Member Author

crazyair commented May 8, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants