mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-24 04:33:10 +00:00
Update "Refs and the DOM" documentation (#2657)
* Update "Refs and the DOM" documentation As function components can now have state through hooks, remove the part that mentions converting a function component to a class component to be able to use state. * Update refs-and-the-dom.md Co-authored-by: Sophie Alpert <git@sophiebits.com>
This commit is contained in:
committed by
Sophie Alpert
parent
3413c785fa
commit
24ebabd01e
@@ -140,7 +140,7 @@ class CustomTextInput extends React.Component {
|
||||
|
||||
#### Refs and Function Components {#refs-and-function-components}
|
||||
|
||||
**You may not use the `ref` attribute on function components** because they don't have instances:
|
||||
By default, **you may not use the `ref` attribute on function components** because they don't have instances:
|
||||
|
||||
```javascript{1,8,13}
|
||||
function MyFunctionComponent() {
|
||||
@@ -161,7 +161,7 @@ class Parent extends React.Component {
|
||||
}
|
||||
```
|
||||
|
||||
You should convert the component to a class if you need a ref to it, just like you do when you need lifecycle methods or state.
|
||||
If you want to allow people to take a `ref` to your function component, you can use [`forwardRef`](https://reactjs.org/docs/forwarding-refs.html) (possibly in conjunction with [`useImperativeHandle`](/docs/hooks-reference.html#useimperativehandle)), or you can convert the component to a class.
|
||||
|
||||
You can, however, **use the `ref` attribute inside a function component** as long as you refer to a DOM element or a class component:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user