Text
@Garry Yao
Developers can hardly resists to a hard life,
which spawn BUG...
Text
var myDivElement = <div>foo</div>;
React.render(myDivElement, mountNode);
Wait...what the heck is that XML?
JSX is a JavaScript syntax extension that looks similar to XML, think of E4X
var myDivElement = <div className="foo">
<input type="text" value="JSX" />
</div>;
React.render(myDivElement, mountNode);
var myDivElement = React.createElement("div",
{className: "foo"},
React.createElement("input",
{type: "text", value: "JSX"})
);
React.render(myDivElement, mountNode);
var isDisabled = true;
React.render(
<button disabled={isDisabled}>Submit</button>, mountNode);
var isDisabled = true;
React.render(React.createElement("button",
{disabled: isDisabled}, "Submit"), mountNode);
React.render(<input type="text" value={isFoo? 'foo' : 'bar'} />, mountNode);
React.render(<select>{
items.map(function(item){
return <option>{item.value}</option>;
})
}</select>, mountNode);
var Hello = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
React.render(<Hello name="John" />, document.body);
a abbr address area article aside audio b base bdi bdo big blockquote body br
button canvas caption cite code col colgroup data datalist dd del details dfn
dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5
h6 head header hr html i iframe img input ins kbd keygen label legend li link
main map mark menu menuitem meta meter nav noscript object ol optgroup option
output p param picture pre progress q rp rt ruby s samp script section select
small source span strong style sub summary sup table tbody td textarea tfoot th
thead time title tr track u ul var video wbr
circle defs ellipse g line linearGradient mask path pattern polygon polyline
radialGradient rect stop svg text tspan
a React Component that
implements the "render" method
with the simplest state management
and intuitive event handlers
var LikeButton = React.createClass({
getInitialState: function() {
return {liked: false};
},
handleClick: function(event) {
this.setState({liked: !this.state.liked});
},
render: function() {
var text = this.state.liked ? 'like' : 'haven\'t liked';
return (
<p onClick={this.handleClick}>
You {text} this. Click to toggle.
</p>
);
}
});
React.render(<LikeButton />, mountNode);
var TodoList = React.createClass({
render: function() {
var createItem = function(itemText) {
return <li>{itemText}</li>;
};
return <ul>{this.props.items.map(createItem)}</ul>;
}
});
var TodoApp = React.createClass({
getInitialState: function() {
return {items: [], text: ''};
},
handleSubmit: function(e) {
...
},
render: function() {
return (
<div>
<h3>TODO</h3>
<TodoList items={this.state.items} />
<form onSubmit={this.handleSubmit}>
<input value={this.state.text} />
<button>{'Add #' + (this.state.items.length + 1)}</button>
</form>
</div>
);
}
});
React.render(<TodoApp />, mountNode);
Just refresh the page!
How about just re-render the component?
var Timer = React.createClass({
getInitialState: function() {
return {secondsElapsed: 0};
},
tick: function() {
this.setState({secondsElapsed: this.state.secondsElapsed + 1});
},
componentDidMount: function() {
this.interval = setInterval(this.tick, 1000);
},
componentWillUnmount: function() {
clearInterval(this.interval);
},
render: function() {
return (
<div>Seconds Elapsed: {this.state.secondsElapsed}</div>
);
}
});
React.render(<Timer />, mountNode);
Every moment your UI is guaranteed to be consistent and update-to-date
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
React.render(<HelloMessage name="John" />, mountNode);
var HelloMessage = React.createClass({displayName: 'HelloMessage',
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});
React.render(React.createElement(HelloMessage, {name: "John"}), mountNode);
In effect, benchmark doesn't really matter if each of the DOM mutation can fit into
Text