June 21, 2020
How to create refs?
There are two approaches
- This is a recently added approach. Refs are created using
React.createRef()
method and attached to React elements via theref
attribute. In order to use refs throughout the component, just assign the ref to the instance property within constructor.
2. You can also use ref callbacks approach regardless of React version. For example, the search bar component's input element accessed as follows,
You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach