-
-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
docs(Form): 如何在函数组件中拿到 form 实例 #19937
Conversation
Deploy preview for ant-design ready! Built with commit 861ff00 |
Added in FAQ instead. |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 861ff00:
|
components/form/index.en-US.md
Outdated
@@ -275,3 +275,55 @@ validator(rule, value, callback) => { | |||
} | |||
} | |||
``` | |||
|
|||
### How you can use `wrappedComponentRef` to get form instance in functional component |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why `wrappedComponentRef` get warning with function component?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean change the title to:
Why
wrappedComponentRef
get warning with function component?
components/form/index.en-US.md
Outdated
|
||
### How you can use `wrappedComponentRef` to get form instance in functional component | ||
|
||
you have to combine `forwardRef` with `useImperativeHandle` so you can get form instance by using `wrappedComponentRef`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should wrap function component with
forwardRef
to pass ref:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useImperativeHandle
is not must have. Please help to check this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://reactjs.org/docs/hooks-reference.html#useimperativehandle, react officially recommend to use useImperativeHandle
with forwardRef
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useImperativeHandle
is aim to customize ref
content. wrappedComponentRef
get the component instance with Form.create. They not use customize ref
indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, you might be right, but in fact, if I remove useImperativeHandle
, I cannot get the instance. you can try the codesandbox.
做一个 codesandbox 吧,这一大段代码略长。 |
components/form/index.en-US.md
Outdated
} | ||
|
||
type Ref = FormComponentProps; | ||
const FCForm = forwardRef<Ref, FCFormProps>(({ form, onSubmit }, ref) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code block should be simplify. Only show how to use forwardRef
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make an codesandbox instead of documenting it.
Codecov Report
@@ Coverage Diff @@
## master #19937 +/- ##
=======================================
Coverage 97.8% 97.8%
=======================================
Files 284 284
Lines 7705 7705
Branches 2120 2165 +45
=======================================
Hits 7536 7536
Misses 169 169 Continue to review full report at Codecov.
|
components/form/index.zh-CN.md
Outdated
@@ -277,3 +277,55 @@ validator(rule, value, callback) => { | |||
} | |||
} | |||
``` | |||
|
|||
### 如何在函数组件中也能正常使用`wrappedComponentRef`拿到 form 实例 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### 如何在函数组件中也能正常使用`wrappedComponentRef`拿到 form 实例 | |
### 如何在函数组件中拿到 form 实例? |
components/form/index.zh-CN.md
Outdated
|
||
### 如何在函数组件中也能正常使用`wrappedComponentRef`拿到 form 实例 | ||
|
||
你需要通过`forwardRef`和`useImperativeHandle`的组合使用来实现在函数组件中正确拿到 form 实例: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你需要通过`forwardRef`和`useImperativeHandle`的组合使用来实现在函数组件中正确拿到 form 实例: | |
你需要通过 `forwardRef` 和 `useImperativeHandle` 的组合使用来实现在函数组件中正确拿到 form 实例: |
好的。 |
中英文解释的不是同一件事情,一个是解决 FC warning,一个是拿 form 实例。需要统一一下。 |
components/form/index.en-US.md
Outdated
@@ -275,3 +275,40 @@ validator(rule, value, callback) => { | |||
} | |||
} | |||
``` | |||
|
|||
### get form instance in functional component |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--- get form instance in functional component
+++ Get form instance from function component
components/form/index.en-US.md
Outdated
|
||
### get form instance in functional component | ||
|
||
you need to combine `forwardRef` with `useImperativeHandle` to get form instance: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y
=> Y
You can combine ...
components/form/index.en-US.md
Outdated
|
||
you need to combine `forwardRef` with `useImperativeHandle` to get form instance: | ||
|
||
your custom form component should look like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y
=> Y
components/form/index.en-US.md
Outdated
}; | ||
``` | ||
|
||
you can click the codesandbox to see the complete demo: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y
=> Y
components/form/index.en-US.md
Outdated
}; | ||
``` | ||
|
||
You can click the codesandbox to see the complete demo: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Online demo:
components/form/index.zh-CN.md
Outdated
}; | ||
``` | ||
|
||
你可以点击下面的 codeSandbox 链接查看完整示例: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在线示例:
components/form/index.en-US.md
Outdated
|
||
You need to combine `forwardRef` with `useImperativeHandle` to get form instance: | ||
|
||
Your custom form component should look like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面有冒号了,这行又一个冒号。感觉可以删掉这行。中文同。
🤔 This is a ...
🔗 Related issue link
💡 Background and solution
there is no a demo that shows developers how they can use
wrappedComponentRef
in functional components. So I made this demo.📝 Changelog
☑️ Self Check before Merge