Good post by Lachlan Young with a simple use case for using createRef
(Class Components) and useRef
(Function Components): focusing an external component (here: TextField
from Material UI) when your own parent component mounts.
I especially like the final thoughts at the end of the post:
If you had no need for a
ref
besides focus, use the following directly:<TextField inputRef={input => input && input.focus()} />
React Refs, both Class and Function Components →
💵 This linked article is stuck behind Medium’s metered paywall, which may prevent you from reading it. Open the link in an incognito window to bypass Medium’s ridiculous reading limit.
💁♂️ Want more? CSS-Tricks has an extensive article on refs.