docs tips small refactorings

This commit is contained in:
Cheng Lou
2013-12-30 17:54:41 -05:00
parent 3b0f705658
commit 1671efb53a
5 changed files with 14 additions and 22 deletions
+2
View File
@@ -16,6 +16,7 @@ var GenericWrapper = React.createClass({
componentDidMount: function() { componentDidMount: function() {
console.log(Array.isArray(this.props.children)); // => true console.log(Array.isArray(this.props.children)); // => true
}, },
render: function() { render: function() {
return <div />; return <div />;
} }
@@ -40,6 +41,7 @@ var GenericWrapper = React.createClass({
// length of the non-existant array wrapper! // length of the non-existant array wrapper!
console.log(this.props.children.length); console.log(this.props.children.length);
}, },
render: function() { render: function() {
return <div />; return <div />;
} }
@@ -22,6 +22,7 @@ var MessageBox = React.createClass({
getInitialState: function() { getInitialState: function() {
return {nameWithQualifier: "Mr. " + this.props.name}; return {nameWithQualifier: "Mr. " + this.props.name};
}, },
render: function() { render: function() {
return <div>{this.state.nameWithQualifier}</div>; return <div>{this.state.nameWithQualifier}</div>;
} }
@@ -44,24 +45,9 @@ var MessageBox = React.createClass({
React.renderComponent(<MessageBox name="Rogers"/>, mountNode); React.renderComponent(<MessageBox name="Rogers"/>, mountNode);
``` ```
For more complex logic: (For more complex logic, simply isolate the computation in a method.)
```js However, it's **not** an anti-pattern if you make it clear that synchronization's not the goal here:
/** @jsx React.DOM */
var MessageBox = React.createClass({
render: function() {
return <div>{this.getNameWithQualifier(this.props.name)}</div>;
},
getNameWithQualifier: function(name) {
return 'Mr. ' + name;
}
});
React.renderComponent(<MessageBox name="Rogers"/>, mountNode);
```
However, it's **not** an anti-pattern if you intentionally make it clear that synchronization's not the goal here:
```js ```js
/** @jsx React.DOM */ /** @jsx React.DOM */
@@ -69,12 +55,14 @@ However, it's **not** an anti-pattern if you intentionally make it clear that sy
var Counter = React.createClass({ var Counter = React.createClass({
getInitialState: function() { getInitialState: function() {
// naming it initialX clearly indicates that the only purpose // naming it initialX clearly indicates that the only purpose
// of the passed down prop is to initialize something internal // of the passed down prop is to initialize something internally
return {count: this.props.initialCount}; return {count: this.props.initialCount};
}, },
handleClick: function() { handleClick: function() {
this.setState({count: this.state.count + 1}); this.setState({count: this.state.count + 1});
}, },
render: function() { render: function() {
return <div onClick={this.handleClick}>{this.state.count}</div>; return <div onClick={this.handleClick}>{this.state.count}</div>;
} }
+2
View File
@@ -21,6 +21,7 @@ var UserGist = React.createClass({
lastGistUrl: '' lastGistUrl: ''
}; };
}, },
componentDidMount: function() { componentDidMount: function() {
$.get(this.props.source, function(result) { $.get(this.props.source, function(result) {
var lastGist = result[0]; var lastGist = result[0];
@@ -30,6 +31,7 @@ var UserGist = React.createClass({
}); });
}.bind(this)); }.bind(this));
}, },
render: function() { render: function() {
return ( return (
<div> <div>
@@ -40,4 +40,4 @@ React.renderComponent(
Notice the use of `bind(this, arg1, arg2, ...)`: we're simply passing more arguments to `handleClick`. This is not a new React concept; it's just JavaScript. Notice the use of `bind(this, arg1, arg2, ...)`: we're simply passing more arguments to `handleClick`. This is not a new React concept; it's just JavaScript.
For communication between two components that don't have a parent-child relationship, you can set up your own global event system. Subscribe to events in `componentDidMount()`, unsubscribe in `componentWillUnmount()`, and when you receive an event call `setState()`. For communication between two components that don't have a parent-child relationship, you can set up your own global event system. Subscribe to events in `componentDidMount()`, unsubscribe in `componentWillUnmount()`, and when you receive an event, call `setState()`.
@@ -17,7 +17,7 @@ var Todo = React.createClass({
render: function() { render: function() {
return <div onClick={this.props.onClick}>{this.props.title}</div>; return <div onClick={this.props.onClick}>{this.props.title}</div>;
}, },
//this component will be accessed by the parent through the `ref` attribute //this component will be accessed by the parent through the `ref` attribute
animate: function() { animate: function() {
console.log('Pretend %s is animating', this.props.title); console.log('Pretend %s is animating', this.props.title);
@@ -28,7 +28,7 @@ var Todos = React.createClass({
getInitialState: function() { getInitialState: function() {
return {items: ['Apple', 'Banana', 'Cranberry']}; return {items: ['Apple', 'Banana', 'Cranberry']};
}, },
handleClick: function(i) { handleClick: function(i) {
var items = this.state.items; var items = this.state.items;
items.splice(i, 1); items.splice(i, 1);
@@ -38,7 +38,7 @@ var Todos = React.createClass({
} }
}.bind(this)); }.bind(this));
}, },
render: function() { render: function() {
return ( return (
<div> <div>