Skip to content

Commit 224b6e5

Browse files
sophiebitszpao
authored andcommitted
Clarify refs and setState callback documentation
(cherry picked from commit 121b290)
1 parent 1170717 commit 224b6e5

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

docs/docs/07.1-more-about-refs.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,19 @@ React supports a very special property that you can attach to any component that
7272

7373
It's as simple as:
7474

75-
**1.** Assign a `ref` attribute to anything returned from `render` such as:
75+
1. Assign a `ref` attribute to anything returned from `render` such as:
7676

77-
```html
78-
<input ref="myInput" />
79-
```
77+
```html
78+
<input ref="myInput" />
79+
```
8080

81-
**2.** In some other code (typically event handler code), access the **backing instance** via `this.refs` as in:
81+
2. In some other code (typically event handler code), access the **backing instance** via `this.refs` as in:
8282

83-
```javascript
83+
```javascript
8484
this.refs.myInput
85-
```
85+
```
86+
87+
You can access the component's DOM node directly by calling `this.refs.myInput.getDOMNode()`.
8688

8789
## Completing the Example
8890

@@ -95,8 +97,11 @@ It's as simple as:
9597
this.setState({userInput: e.target.value});
9698
},
9799
clearAndFocusInput: function() {
98-
this.setState({userInput: ''}); // Clear the input
99-
this.refs.theInput.getDOMNode().focus(); // Boom! Focused!
100+
// Clear the input
101+
this.setState({userInput: ''}, function() {
102+
// This code executes after the component is re-rendered
103+
this.refs.theInput.getDOMNode().focus(); // Boom! Focused!
104+
});
100105
},
101106
render: function() {
102107
return (
@@ -132,4 +137,4 @@ Refs are a great way to send a message to a particular child instance in a way t
132137

133138
- *Never* access refs inside of any component's render method - or while any component's render method is even running anywhere in the call stack.
134139
- If you want to preserve Google Closure Compiler Crushing resilience, make sure to never access as a property what was specified as a string. This means you must access using `this.refs['myRefString']` if your ref was defined as `ref="myRefString"`.
135-
- If you have not programmed several apps with React, your first inclination is usually going to be to try 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. Placing the state there often eliminates any desire to use `ref`s to "make things happen" - instead, the data flow will usually accomplish your goal.
140+
- If you have not programmed several apps with React, your first inclination is usually going to be to try 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. Placing the state there often eliminates any desire to use `ref`s to "make things happen" instead, the data flow will usually accomplish your goal.

docs/docs/ref-02-component-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ setProps(object nextProps[, function callback])
2929

3030
When you're integrating with an external JavaScript application you may want to signal a change to a React component rendered with `React.renderComponent()`.
3131

32-
Though calling `React.renderComponent()` again on the same node is the preferred way to update a root-level component, you can also call `setProps()` to change its properties and trigger a re-render. In addition, you can supply an optional callback function that is executed once `setProps` is completed.
32+
Though calling `React.renderComponent()` again on the same node is the preferred way to update a root-level component, you can also call `setProps()` to change its properties and trigger a re-render. In addition, you can supply an optional callback function that is executed once `setProps` is completed and the component is re-rendered.
3333

3434
> Note:
3535
>
@@ -80,7 +80,7 @@ Properties that are specified directly on the target component instance (such as
8080
setState(object nextState[, function callback])
8181
```
8282

83-
Merges nextState with the current state. This is the primary method you use to trigger UI updates from event handlers and server request callbacks. In addition, you can supply an optional callback function that is executed once `setState` is completed.
83+
Merges nextState with the current state. This is the primary method you use to trigger UI updates from event handlers and server request callbacks. In addition, you can supply an optional callback function that is executed once `setState` is completed and the component is re-rendered.
8484

8585
> Notes:
8686
>

0 commit comments

Comments
 (0)