Nordnet Academy
A Javascript library for building user interfaces
React documentation
var Hello = React.createClass({
render: function() {
return (
<div>Hello {this.props.name}</div>
);
}
});
<a class="toggle">Toggle</a>
$('.toggle').click(function() {
$(this).toggle();
}
// Angular
<div ng-repeat="account in vm.accounts">
var Hello = React.createClass({
render: function() {
return (
<div>Hello {this.props.name}</div>
);
}
});
React components are idempotent functions. They describe your UI at any point in time, just like a server-rendered app.
~ Pete Hunt, React: Rethinking best practices
var Hello = React.createClass({
render: function() {
return (
<div>
Hello {this.props.name}
</div>
);
}
});
class Hello extends React.Component {
render() {
return (
<div>
Hello {this.props.name}
</div>
);
}
}
React is all about modular, composable components
class Comment extends React.Component {
render() {
const { text, author } = this.props.comment;
return (
<div>
{ text }, { author }
</div>
);
}
}
class Comment extends React.Component {
render() {
const { text, author } = this.props.comment;
return (
React.createElement("div", null, text, ", ", author)
);
}
}
class CommentsList extends React.Component {
render() {
const comments = this.props.comments.map(
comment => <Comment key={comment.id} comment={comment} />);
return (
<div>
{ comments }
</div>
);
}
}
class CommentsList extends React.Component {
render() {
const comments = this.props.comments.map(
comment =>
React.createElement(
Comment, {key: comment.id, comment: comment})
);
return (
React.createElement("div", null, comments)
);
}
}
class Application extends React.Component {
render() {
const comments = [
{ id: 1, text: 'First comment', author: 'dmidem' },
{ id: 2, text: 'Second comment', author: 'dmidem' },
];
return (
<CommentsList comments={comments} />
);
}
}
React.render(<Application />, document.getElementById('app'));
class Application extends React.Component {
render() {
const comments = [
{ id: 1, text: 'First comment', author: 'dmidem' },
{ id: 2, text: 'Second comment', author: 'dmidem' },
];
return (
React.createElement(CommentsList, {comments: comments})
);
}
}
React.render(
React.createElement(Application, null),
document.getElementById('app'));
Build components, not templates
~ Pete Hunt, React: Rethinking best practices
React.createClass()
extends React.Component
React.createClass()
extends React.Component
var Hello = React.createClass({
getDefaultProps: function() {
return { name: 'stranger' };
},
getInitialState: function() {
return { greeting: 'Hello' };
},
render: function() {
return (
<div>
{ this.state.greeting }
{ this.props.name }
</div>
);
}
});
React.render(<Hello name="dmidem" />,
element);
class Hello extends React.Component {
constructor(props) {
super(props);
this.state = { greeting: 'Hello' };
}
render() {
return (
<div>
{ this.state.greeting }
{ this.props.name }
</div>
);
}
}
Hello.defaultProps = { name: 'stranger' };
React.render(<Hello name="dmidem" />,
element);
React documentation
The algorithm will not try to match sub-trees of different components classes. If you see yourself alternating between two components classes with very similar output, you may want to make it the same class.
If you don't provide stable keys (e.g. by using Math.random()), all the sub-trees are going to be re-rendered every single time. By giving the users the choice to choose the key, they have the ability to shoot themselves in the foot.