refs
Last updated
Last updated
When working with React, you usally are working with virtual DOM elements that react would finally render to actual DOM as needed. However, there are some cases where you might have to interact with the actual elements. For such cases, React provides a ref system.
In React, is most cases avoid the need to use access elements after DOM rendering,
But when unavoidable, use Refs not Ids (why? - see )
createRef
method ( in React 16.3+)Create an class-level ref
variable using React.createRef()
Assign the ref
attribute of the element that you want to access to in DOM to the above created ref variable.
After the component has been rendered/mounted, the element's DOM reference can be accessed using the current
property of the ref variable.
just pass a callback that assigns to a property: