Espen @
Made by
Read: build step "required"
render: function() {
React.DOM.div({ className: 'my-thing' },
React.DOM.a({ href: '/path' },
'Link: ' + this.props.linkText
)
);
}
render: function() {
return (
<div className="my-thing">
<a href="/path">
Link: {this.props.linkText}
</a>
</div>
);
}
Render on every change!
wow
amaze
var EventListItem = React.createClass({
render: function() {
var e = this.props.event;
return (
<li>
<div className={'icon icon-' + e.type} />
<div className="minute">{e.minute}</div>
<div className="text">{e.text}</div>
</li>
);
}
});
var EventList = React.createClass({
render: function() {
var events = this.props.events;
if (this.props.match.isFinished) {
events = events.reverse();
}
return (
<ul className="event-list">
{events.map(EventListItem)}
</ul>
);
}
});
Virtual DOM
<li>
<div class="icon icon-yellowcard"></div>
<div class="minute">30</div>
<div class="text">Gult kort: BIRGER HANSEN.</div>
</li>
<li>
<div class="icon icon-redcard"></div>
<div class="minute">30</div>
<div class="text">Rødt kort: BIRGER HANSEN.</div>
</li>
vs
=
li.children[0].className = 'icon icon-redcard';
li.children[2].innerText = 'Rødt kort: BIRGER HANSEN.';
FAST
FAST
Minimal reflows
var EventList = require('path/to/event-list-component');
var SomeApi = require('path/to/some-api');
var Server = require('someHttpServer').listen(80);
Server.on('request', function(req, res) {
SomeApi.getEvents(req.params.matchId)
.then(function(err, events) {
var html = React.renderComponentToString(
new EventList({ events: events })
);
res.send(html);
});
});
Client side can now diff the tree and continue updating state on changes!
React.createClass({
onClick: function(e) {
// "this" refers to the component! yay!
this.setState({ clicked: true });
},
render: function() {
return <button onClick={this.onClick} />;
}
});
Triggers re-render of component.
Espen
Hovlandsdal