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

docs(Form): 如何在函数组件中拿到 form 实例 #19937

Merged
merged 9 commits into from
Nov 27, 2019
Merged

docs(Form): 如何在函数组件中拿到 form 实例 #19937

merged 9 commits into from
Nov 27, 2019

Conversation

chj-damon
Copy link
Contributor

@chj-damon chj-damon commented Nov 26, 2019

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / document update
  • Component style update
  • TypeScript definition update
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

🔗 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

Language Changelog
🇺🇸 English update form document FAQ with using wrappedComponentRef in functional component
🇨🇳 Chinese 更新Form文档,在FAQ增加如何在函数组件中使用wrappedComponentRef的例子

☑️ Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@netlify
Copy link

netlify bot commented Nov 26, 2019

Deploy preview for ant-design ready!

Built with commit 861ff00

https://deploy-preview-19937--ant-design.netlify.com

@zombieJ
Copy link
Member

zombieJ commented Nov 26, 2019

Added in FAQ instead.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 26, 2019

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:

Sandbox Source
antd reproduction template Configuration

@@ -275,3 +275,55 @@ validator(rule, value, callback) => {
}
}
```

### How you can use `wrappedComponentRef` to get form instance in functional component
Copy link
Member

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?

Copy link
Contributor Author

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()

Copy link
Member

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?


### 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`:
Copy link
Member

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:

Copy link
Member

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.

Copy link
Contributor Author

@chj-damon chj-damon Nov 26, 2019

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.

Copy link
Member

@zombieJ zombieJ Nov 26, 2019

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.

Copy link
Contributor Author

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.

@afc163
Copy link
Member

afc163 commented Nov 26, 2019

做一个 codesandbox 吧,这一大段代码略长。

}

type Ref = FormComponentProps;
const FCForm = forwardRef<Ref, FCFormProps>(({ form, onSubmit }, ref) => {
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link

codecov bot commented Nov 26, 2019

Codecov Report

Merging #19937 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           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.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ae44aae...861ff00. Read the comment docs.

@@ -277,3 +277,55 @@ validator(rule, value, callback) => {
}
}
```

### 如何在函数组件中也能正常使用`wrappedComponentRef`拿到 form 实例
Copy link
Member

@afc163 afc163 Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 如何在函数组件中也能正常使用`wrappedComponentRef`拿到 form 实例
### 如何在函数组件中拿到 form 实例


### 如何在函数组件中也能正常使用`wrappedComponentRef`拿到 form 实例

你需要通过`forwardRef`和`useImperativeHandle`的组合使用来实现在函数组件中正确拿到 form 实例:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
你需要通过`forwardRef``useImperativeHandle`的组合使用来实现在函数组件中正确拿到 form 实例:
你需要通过 `forwardRef``useImperativeHandle` 的组合使用来实现在函数组件中正确拿到 form 实例:

@chj-damon
Copy link
Contributor Author

chj-damon commented Nov 26, 2019

做一个 codesandbox 吧,这一大段代码略长。

好的。

@chj-damon chj-damon changed the title 为Form新增如何在函数组件中使用wrappedComponentRef的例子 如何在函数组件中拿到 form 实例 Nov 26, 2019
@yoyo837 yoyo837 changed the title 如何在函数组件中拿到 form 实例 docs: 如何在函数组件中拿到 form 实例 Nov 27, 2019
@yoyo837 yoyo837 changed the title docs: 如何在函数组件中拿到 form 实例 docs(Form): 如何在函数组件中拿到 form 实例 Nov 27, 2019
@zombieJ
Copy link
Member

zombieJ commented Nov 27, 2019

中英文解释的不是同一件事情,一个是解决 FC warning,一个是拿 form 实例。需要统一一下。

@@ -275,3 +275,40 @@ validator(rule, value, callback) => {
}
}
```

### get form instance in functional component
Copy link
Member

@zombieJ zombieJ Nov 27, 2019

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


### get form instance in functional component

you need to combine `forwardRef` with `useImperativeHandle` to get form instance:
Copy link
Member

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 ...


you need to combine `forwardRef` with `useImperativeHandle` to get form instance:

your custom form component should look like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y => Y

};
```

you can click the codesandbox to see the complete demo:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

y => Y

};
```

You can click the codesandbox to see the complete demo:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Online demo:

};
```

你可以点击下面的 codeSandbox 链接查看完整示例:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在线示例:


You need to combine `forwardRef` with `useImperativeHandle` to get form instance:

Your custom form component should look like this:
Copy link
Member

@zombieJ zombieJ Nov 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面有冒号了,这行又一个冒号。感觉可以删掉这行。中文同。

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

Successfully merging this pull request may close these issues.

None yet

3 participants