Tiago Rodrigues
@trodrigues
JS/Frontend developer at contentful.com
// ES6 modules
import React, { Component } from 'react';
// ES6 classes
export default class App extends Component {
render() {
return (
<h1>Hello, world.</h1>
);
}
}
import React, { Component } from 'react';
export default class MyFirstComponent extends Component {
render() {
return (
<p>This is my first component</p>
);
}
}
Branch: step1
import React, { Component } from 'react';
import './MyFirstComponent';
export default class App extends Component {
render() {
// This won't really work
return (
<h1>Hello, world.</h1>
<MyFirstComponent />
);
}
}
Branch: step1
import React, { Component } from 'react';
import './MyFirstComponent';
export default class App extends Component {
render() {
return (
<div>
<h1>Hello, world.</h1>
<MyFirstComponent />
</div>
);
}
}
Branch: step1
export default class MyFirstComponent extends Component {
render() {
return (
<button onClick={this.handleClick}>Do stuff</button>
);
}
handleClick() {
alert('Stuff is done!');
}
}
Branch: step2
export default class MyFirstComponent extends Component {
constructor(props){
super(props);
this.state = { count: 0 };
}
render() {
return (
<div>
<p>Clicked {this.state.count} times</p>
<button onClick={this.handleClick.bind(this)}>Do stuff</button>
</div>
);
}
handleClick() {
this.setState({
count: this.state.count + 1
});
}
};
Branch: step3
export default class MyFirstComponent extends Component {
constructor(props){
super(props);
this.state = {
items: [
{label: 'item 1', quantity: 4},
{label: 'item 2', quantity: 3}
]
};
}
render() {
var total = this.state.items.reduce((total, item) => total+item.quantity, 0);
return (
<p>Total: {total}</p>
);
}
};
Branch: step4
export default class MyFirstComponent extends Component {
constructor(props){
super(props);
this.state = {
items: [
{label: 'item 1', quantity: 4},
{label: 'item 2', quantity: 3}
]
};
}
render() {
var items = this.state.items.map(item => {
return <li>Item: {item.label}, Quantity: {item.quantity}</li>;
});
return (
<ul>{items}</ul>
);
}
};
Branch: step5
import React, { Component } from 'react';
import MyFirstComponent from './MyFirstComponent';
export default class App extends Component {
render() {
return (
<MyFirstComponent message="hello world" />
);
}
}
Branch: step6
import React, { Component } from 'react';
export default class MyFirstComponent extends Component {
render() {
return (
<h1>{this.props.message}</h1>
);
}
};
Branch: step6
import React, { Component } from 'react';
import axios from 'axios';
const API_URL = 'http://pokeapi.co/';
const API_PATH = API_URL + 'api/v1/';
export default class Pokemon extends Component {
constructor(props){
super(props);
this.state = {name: '', image: ''};
}
componentDidMount() {...}
render() {
return (
<div>
<h1>{this.state.name}</h1>
<img src={this.state.image} />
</div>
);
}
};
Branch: step7
import axios from 'axios';
const API_URL = 'http://pokeapi.co/';
const API_PATH = API_URL + 'api/v1/';
export default class Pokemon extends Component {
constructor(props){...}
componentDidMount() {
axios.get(API_PATH + `pokemon/${this.props.id}/`)
.then(response => {
var name = response.data.name;
axios.get(API_URL + response.data.sprites[0].resource_uri)
.then(response => this.setState({
name: name,
image: API_URL + response.data.image
}));
});
}
render() {...}
};
Branch: step7
/* ... */
export default class Pokemon extends Component {
constructor(props){
super(props);
this.state = {name: '', image: ''};
}
componentDidMount() {
axios.get(API_PATH + `pokemon/${this.props.id}/`)
.then(response => {
var name = response.data.name;
axios.get(API_URL + response.data.sprites[0].resource_uri)
.then(response => this.setState({
name: name,
image: API_URL + response.data.image
}));
});
}
render() {
return (
<PokemonDisplay name={this.state.name} image={this.state.image} />
);
}
};
Branch: step8
import React, { Component } from 'react';
export default class PokemonDisplay extends Component {
render() {
return (
<div>
<h1>{this.state.name}</h1>
<img src={this.state.image} />
</div>
);
}
};
Branch: step8
var TodoStore = {
__proto__: EventEmitter.prototype,
_todos: [],
getAll: function() {
return this._todos;
},
emitChange: function() {
this.emit('change');
},
addChangeListener: function(callback) {
this.on('change', callback);
}
};
...
...
AppDispatcher.register(function(action) {
switch(action.actionType) {
case TodoConstants.TODO_CREATE:
TodoStore._todos.push(action.text);
TodoStore.emitChange();
break;
}
});
var TodoActions = {
create(text) {
AppDispatcher.dispatch({
actionType: TodoConstants.TODO_CREATE,
text: text
});
}
};
More at:
http://slides.com/trodrigues/flux-implementations
Thank you!