|
| 1 | +The significance of keys and refs. The names of these attributes speak for themselves: both keys and refs are used to identify particular elements in the DOM, however their purposes are different. |
| 2 | + |
| 3 | +[Official Doc - Refs and the DOM](https://reactjs.org/docs/refs-and-the-dom.html) |
| 4 | + |
| 5 | +#### Sometimes when using React.js you’ll need an escape hatch to write imperative-style code to interact directly with DOM elements. Using React’s createRef method allows you to do just that! |
| 6 | + |
| 7 | +#### React provides a way to get references to DOM nodes by using React.createRef(). It’s really just an equivalent of this all-too-familiar snippet of JavaScript: |
| 8 | + |
| 9 | +`document.getElementById('foo-id');` |
| 10 | + |
| 11 | +This is exactly what React.createRef() does, although it requires a bit of a different setup. |
| 12 | + |
| 13 | +### refs are used to get reference to a DOM node or an instance of a component in a React Application i.e. refs would return the node we are referencing . |
| 14 | + |
| 15 | +Similarly to keys refs are added to elements in the form of attributes. According to React.js documentation some of the best cases for using refs are: managing focus, text selection, or media playback, triggering animations, and integrating with third-party DOM libraries. |
| 16 | + |
| 17 | +In the typical React dataflow, props are the only way that parent components interact with their children. To modify a child, you re-render it with new props. However, there are a few cases where you need to imperatively modify a child outside of the typical dataflow. In other words, in some cases you might need to modify a child without re-rendering it with new props. That’s exactly when refs attribute comes to use. |
| 18 | +The child to be modified could be an instance of a React component, or it could be a DOM element. For both of these cases, React provides an escape hatch. |
| 19 | + |
| 20 | +[https://alligator.io/react/createref/](https://alligator.io/react/createref/) |
| 21 | + |
| 22 | +```js |
| 23 | +class Foobar extends Component { |
| 24 | + constructor(props) { |
| 25 | + super(props); |
| 26 | + this.myInput = React.createRef(); // initialize "this.myInput" |
| 27 | + } |
| 28 | + |
| 29 | + render() { |
| 30 | + return ( |
| 31 | + <input ref={this.myInput}/> {/* pass "this.myInput" as prop */} |
| 32 | + ); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +#### All standard HTML elements in React have a reserved prop called ref (much like style which is a reserved prop). Simply pass the ref you initialized in the constructor to the ref prop… and voila! You can start interacting with the <input> DOM node by using this.myInput.current |
| 39 | + |
| 40 | +`this.myInput.current` holds the reference to the DOM node |
| 41 | + |
| 42 | +Example: Focusing an <input> |
| 43 | +Taking that last code snippet, let’s make the most common use-case of createRef() to demonstrate how we could start interacting with the <input> DOM node: |
| 44 | + |
| 45 | +```js |
| 46 | +export default class App extends Component { |
| 47 | + constructor(props) { |
| 48 | + super(props) |
| 49 | + this.myInput = React.createRef() |
| 50 | + } |
| 51 | + render() { |
| 52 | + return ( |
| 53 | + <div> |
| 54 | + <input ref={this.myInput} /> |
| 55 | + |
| 56 | + <button |
| 57 | + onClick={() => { |
| 58 | + this.myInput.current.focus() |
| 59 | + }} |
| 60 | + > |
| 61 | + focus! |
| 62 | + </button> |
| 63 | + </div> |
| 64 | + ) |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +#### Further Reading |
| 70 | + |
| 71 | +[https://moduscreate.com/blog/everything-you-need-to-know-about-refs-in-react/](https://moduscreate.com/blog/everything-you-need-to-know-about-refs-in-react/) |
| 72 | + |
| 73 | +[All refs in one place](https://react-refs-cheatsheet.netlify.com/) |
0 commit comments