You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+163-2Lines changed: 163 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ React.render(
94
94
95
95
## this.props.children
96
96
97
-
we use`this.props.children` to access a component's children.
97
+
React uses`this.props.children` to access a component's children.
98
98
99
99
```js
100
100
var NotesList =React.createClass({
@@ -120,6 +120,166 @@ React.render(
120
120
);
121
121
```
122
122
123
+
## Finding a DOM node
124
+
125
+
React uses React.findDOMNode() to find a DOM node.
126
+
127
+
```js
128
+
var MyComponent =React.createClass({
129
+
handleClick:function() {
130
+
React.findDOMNode(this.refs.myTextInput).focus();
131
+
},
132
+
render:function() {
133
+
return (
134
+
<div>
135
+
<input type="text" ref="myTextInput"/>
136
+
<input type="button" value="Focus the text input" onClick={this.handleClick} />
137
+
</div>
138
+
);
139
+
}
140
+
});
141
+
142
+
React.render(
143
+
<MyComponent />,
144
+
document.getElementById('example')
145
+
);
146
+
```
147
+
148
+
## this.state
149
+
150
+
React thinks of component as state machines, and uses `this.state` to hold component's state, `this.setState()` to update `this.state` and re-render the component.
151
+
152
+
```js
153
+
var LikeButton =React.createClass({
154
+
getInitialState:function() {
155
+
return {liked:false};
156
+
},
157
+
handleClick:function(event) {
158
+
this.setState({liked:!this.state.liked});
159
+
},
160
+
render:function() {
161
+
var text =this.state.liked?'like':'haven\'t liked';
162
+
return (
163
+
<p onClick={this.handleClick}>
164
+
You {text} this. Click to toggle.
165
+
</p>
166
+
);
167
+
}
168
+
});
169
+
170
+
React.render(
171
+
<LikeButton />,
172
+
document.getElementById('example')
173
+
);
174
+
```
175
+
176
+
## Form
177
+
178
+
The `value` property of Form components, such as <input>, <textarea>, and <option>, is unaffected by any user input. If you wanted to update the value in response to user input, you could use the onChange event.
Components have three main parts of their lifecycle: Mounting, Updating and Unmounting. React provides hooks into these lifecycle part. `will` methods are called right before something happens, and `did` methods which are called right after something happens.
205
+
206
+
```js
207
+
var Hello =React.createClass({
208
+
getInitialState:function () {
209
+
return {
210
+
opacity:1.0
211
+
};
212
+
},
213
+
214
+
componentDidMount:function () {
215
+
this.timer=setInterval(function () {
216
+
var opacity =this.state.opacity;
217
+
opacity -=.05;
218
+
if (opacity <0.1) {
219
+
opacity =1.0;
220
+
}
221
+
this.setState({
222
+
opacity: opacity
223
+
});
224
+
}.bind(this), 100);
225
+
},
226
+
227
+
render:function () {
228
+
return (
229
+
<div style={{opacity:this.state.opacity}}>
230
+
Hello {this.props.name}
231
+
</div>
232
+
);
233
+
}
234
+
});
235
+
236
+
React.render(
237
+
<Hello name="world"/>,
238
+
document.body
239
+
);
240
+
```
241
+
242
+
## Ajax
243
+
244
+
Fetch data in componentDidMount. When the response arrives, store the data in state, triggering a render to update your UI.
0 commit comments