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
Please be minded that only if the component has more than one child node, you could take `this.props.children` as an array, otherwise `this.props.children.map` throws a TypeError.
154
155
155
-
## Demo6: Finding a DOM node ([source](https://github.com/ruanyf/react-demos/blob/master/demo06/index.html))
Components have many specific attributes which are called ”props” in React and can be of any type.
159
+
160
+
Sometimes you need a way to validate these props. You don't want users input anything into your components.
161
+
162
+
React has a solution for this and it's called propTypes.
163
+
164
+
```javascript
165
+
var MyTitle =React.createClass({
166
+
propTypes: {
167
+
title:React.PropTypes.string.isRequired,
168
+
},
169
+
170
+
render:function() {
171
+
return<h1> {this.props.title} </h1>;
172
+
}
173
+
});
174
+
```
175
+
176
+
The above component of `Mytitle` has a props of `title`. PropTypes tells React that title is required and its value should be string.
177
+
178
+
Now we give `Title` a number value.
179
+
180
+
```javascript
181
+
var data =123;
182
+
183
+
React.render(
184
+
<MyTitle title={data} />,
185
+
document.body
186
+
);
187
+
```
188
+
189
+
It means the props doesn't pass the validation, and the console will show you a error message.
190
+
191
+
```bash
192
+
Warning: Failed propType: Invalid prop `title` of type`number` supplied to `MyTitle`, expected `string`.
193
+
```
194
+
195
+
Visit [official doc](http://facebook.github.io/react/docs/reusable-components.html) for more PropTypes options.
196
+
197
+
P.S. If you want to give the props a default value, use `getDefaultProps()`.
198
+
199
+
```javascript
200
+
var MyTitle =React.createClass({
201
+
getDefaultProps:function () {
202
+
return {
203
+
title :'Hello World'
204
+
};
205
+
},
206
+
207
+
render:function() {
208
+
return<h1> {this.props.title} </h1>;
209
+
}
210
+
});
211
+
212
+
React.render(
213
+
<MyTitle />,
214
+
document.body
215
+
);
216
+
```
217
+
218
+
## Demo07: Finding a DOM node ([source](https://github.com/ruanyf/react-demos/blob/master/demo07/index.html))
156
219
157
220
Sometimes you need to reference a DOM node in a component. React gives you React.findDOMNode() to find it.
158
221
@@ -179,7 +242,7 @@ React.render(
179
242
180
243
The desired DOM node should have a `ref` attribute, and `React.findDOMNode(this.refs.[refName])` would return the corresponding DOM node. Please be minded that you could do that only after this component has been mounted into the DOM, otherwise you get `null`.
React thinks of component as state machines, and uses `this.state` to hold component's state, `getInitialState()` to initialize `this.state`(invoked before a component is mounted), `this.setState()` to update `this.state` and re-render the component.
185
248
@@ -209,7 +272,7 @@ React.render(
209
272
210
273
You could use component attributes to register event handlers, just like `onClick`, `onKeyDown`, `onCopy`, etc. Official Document has all [supported events](http://facebook.github.io/react/docs/events.html#supported-events).
211
274
212
-
## Demo08: Form ([source](https://github.com/ruanyf/react-demos/blob/master/demo08/index.html))
275
+
## Demo09: Form ([source](https://github.com/ruanyf/react-demos/blob/master/demo09/index.html))
213
276
214
277
According to React's design philosophy, `this.state` describes the state of component and is mutated via user interactions, and `this.props` describes the properties of component and is stable and immutable.
Components have three main parts of [their lifecycle](https://facebook.github.io/react/docs/working-with-the-browser.html#component-lifecycle): Mounting(being inserted into the DOM), Updating(being re-rendered) and Unmounting(being removed from the DOM). 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.
245
308
@@ -289,7 +352,7 @@ The following is [a whole list of lifecycle methods](http://facebook.github.io/r
289
352
- componentWillReceiveProps(object nextProps): invoked when a mounted component receives new props.
290
353
- shouldComponentUpdate(object nextProps, object nextState): invoked when a component decides whether any changes warrant an update to the DOM.
How to get the data of a component from a server or an API provider? The answer is using Ajax to fetch data in the event handler of `componentDidMount`. When the server response arrives, store the data with `this.setState()` to trigger a re-render of your UI.
This demo is copied from [github.com/mhart/react-server-example](https://github.com/mhart/react-server-example), but I rewrote it with JSX syntax.
336
399
@@ -339,7 +402,7 @@ This demo is copied from [github.com/mhart/react-server-example](https://github.
339
402
$ npm install
340
403
341
404
# translate all jsx file in src subdirectory to js file
342
-
$ jsx src/ .
405
+
$ npm run build
343
406
344
407
# launch http server
345
408
$ node server.js
@@ -351,16 +414,16 @@ $ node server.js
351
414
352
415
All above demos don't use JSX compilation forclarity. In production environment, ensure to precompile JSX files before putting them online.
353
416
354
-
First, install the command-line tools.
417
+
First, install the command-line tools [Babel](https://babeljs.io/docs/usage/cli/).
355
418
356
419
```bash
357
-
$ npm install -g react-tools
420
+
$ npm install -g babel
358
421
```
359
422
360
-
Then precompile your JSXfiles(.jsx) into JavaScript(.js).
423
+
Then precompile your JSXfiles(.jsx) into JavaScript(.js).Compiling the entire src directory and output it to the build directory, you may use the option --out-dir or -d.
361
424
362
425
```bash
363
-
$ jsx -x src/ build/
426
+
$ babel src --out-dir build
364
427
```
365
428
366
429
Put the compiled JS files into HTML.
@@ -371,7 +434,7 @@ Put the compiled JS files into HTML.
0 commit comments