React Refs, both Class and Function Components

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.

Published by Bramus!

Bramus is a Freelance Web Developer from Belgium. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.