diff --git a/content/docs/refs-and-the-dom.md b/content/docs/refs-and-the-dom.md index ca8e52c9e..4d31a1054 100644 --- a/content/docs/refs-and-the-dom.md +++ b/content/docs/refs-and-the-dom.md @@ -11,6 +11,8 @@ redirect_from: permalink: docs/refs-and-the-dom.html --- +Refs provide a way to access DOM nodes or React elements created in the render method. + In the typical React dataflow, [props](/docs/components-and-props.html) 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. 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. ### When to Use Refs @@ -29,32 +31,69 @@ For example, instead of exposing `open()` and `close()` methods on a `Dialog` co Your first inclination may be to use refs to "make things happen" in your app. If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy. Often, it becomes clear that the proper place to "own" that state is at a higher level in the hierarchy. See the [Lifting State Up](/docs/lifting-state-up.html) guide for examples of this. -### Adding a Ref to a DOM Element +> Note +> +> The examples below have been updated to use the React.createRef() API introduced in React 16.3. If you are using an earlier release of React, we recommend using [#callback-refs](callback refs) instead. + +### Creating Refs + +Refs are created using `React.createRef()` and attached to React elements via the `ref` attribute. Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the the component. + +```javascript{4,7} +class MyComponent extends React.Component { + constructor(props) { + super(props); + this.myRef = React.createRef(); + } + render() { + return
; + } +} +``` + +### Accessing Refs + +When a ref is passed to an element in `render`, a reference to the node becomes accessible at the `value` attribute of the ref. -React supports a special attribute that you can attach to any component. The `ref` attribute takes a callback function, and the callback will be executed immediately after the component is mounted or unmounted. +```javascript +const node = this.myRef.value +``` + +The value of the ref differs depending on the type of the node: + +- When the `ref` attribute is used on an HTML element, the `ref` created in the constructor with `React.createRef()` receives the underlying DOM element as its `value` property. +- When the `ref` attribute is used on a custom class component, the `ref` object receives the mounted instance of the component as its `value`. +- **You may not use the `ref` attribute on functional components** because they don't have instances. + +The examples below demonstrate the differences. -When the `ref` attribute is used on an HTML element, the `ref` callback receives the underlying DOM element as its argument. For example, this code uses the `ref` callback to store a reference to a DOM node: +#### Adding a Ref to a DOM Element -```javascript{8,9,19} +This code uses a `ref` to store a reference to a DOM node: + +```javascript{5,12,22} class CustomTextInput extends React.Component { constructor(props) { super(props); + // create a ref to store the textInput DOM element + this.textInput = React.createRef(); this.focusTextInput = this.focusTextInput.bind(this); } focusTextInput() { // Explicitly focus the text input using the raw DOM API - this.textInput.focus(); + // Note: we're accessing "value" to get the DOM node + this.textInput.value.focus(); } render() { - // Use the `ref` callback to store a reference to the text input DOM - // element in an instance field (for example, this.textInput). + // tell React that we want the associate the ref + // with the `textInput` that we created in the constructor return (