Burlington JS
John, Matt, Donnie
Full-stack dev and team lead working on custom web applications.
Part-time zymurgist, full-time dog parent
*Interested in writing apps in React with ES6?
We are hiring front-end and back-end devs!
Abstract: The data required to render a given user interface.
Concrete: JSON objects.
const state = {
comments: [
{id: 1, text: 'It was great'},
{id: 2, text: 'It was meh'}
],
loading: false
};
A library for creating user interfaces
with composable components.
Used by Facebook, Instagram, Netflix, Yahoo!, Khan Academy, Codecademy.
Less Like: Rendering HTML (Handlebars, jade, haml).
More Like: Re-usable Components w/ configurable behavior (Angular Directives, jQuery UI Widgets, Web Components)
What will this component render? What behavior will it exhibit?
A component is a small composable unit of a user interface.
A component knows how to output DOM based on state.
Smaller components can be composed together to create more complex user interfaces because they nest like HTML tags.
Kinda Like: Angular directives, jQuery UI Widgets, Web Components
"If your gonna make a component,
you gotta have a render in the class."
For many components this is all you need.
To render a component, we use React.render to
mount our component to a DOM node
<MyComponent propname="value" >
<button onClick={this.handleClick.bind(this)}>GO!</button>
render() {
return <div className="comments"><Comment text="Hello" /></div>
}
{
type: 'div',
props: {
className: 'comments',
children: [
{
type: function Comment,
props: {
text: 'Hello'
children: []
}
}
]
}
}
*There are many Flux implementations out there: Alt, Reflux, Fluxible, Redux, McFly, Delorean, Om (ClojureScript) ... seemingly all with pun-y names
const commentApi = {
addComment({ newComment }) {
return new Promise(function(resolve, reject) {
if (newComment.text && newComment.author) {
resolve({ newComment: newComment });
} else {
reject({ newComment: newComment, errors: ['Please enter text and an author']});
}
});
}
};
commentActions.addComment.listen(function(newComment) {
commentApi.addComment(newComment)
.then(commentActions.addCommentCompleted)
.catch(commentActions.addCommentFailed);
});
const commentStore = {
data: { errors: [], loading: false, comments: [] },
init() {
commentActions.addComment.listen(this.addComment);
commentActions.addCommentCompleted.listen(this.addComment);
commentActions.addCommentFailed.listen(this.addComment);
},
addComment() {
this.data.loading = true;
this.trigger(this.data);
},
addCommentCompleted({newComment}) {
this.data.comments = this.data.comments.concat([newComment]);
this.data.loading = false;
this.data.errors = [];
this.trigger(this.data);
},
addCommentCompleted({newComment, errors}) {
this.data.loading = false;
this.data.errors = errors;
this.trigger(this.data);
}
};