refs
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 https://www.javascriptstuff.com/use-refs-not-ids/)
Creating a Ref using createRef
method ( in React 16.3+)
createRef
method ( in React 16.3+)Create an class-level
ref
variable usingReact.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.
Creating a Ref using callback Refs ( in older React versions)
just pass a callback that assigns to a property:
Last updated