Some of the ES6+ features are great companions when using React. This presentation introduces you to some of them.
This talk was inspired by Steven Luscher's blog post: http://babeljs.io/blog/2015/06/07/react-on-es6-plus/
2. React on ES6+ @nikgraf
Nik Graf
@nikgraf
nik@nikgraf.com
Creator of Belle
Working with StarterSquad
3. React on ES6+ @nikgraf
ECMAScript 5
December 2009 - ECMAScript 5 was published
4. React on ES6+ @nikgraf
Why bother about ES6+?
Classes
Enhanced Object Literals
Block-scoped binding constructs (let + const)
Property Initialisers
Arrow Functions
Template Strings
Spread Attributes
Deconstructing Attributes
Generators
DataStructures (Map, Set, WeakMap, WeakSet)
… and many more
5. React on ES6+ @nikgraf
ES6 is finalised
Final Draft April 14, 2015 Rev 38
http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts#final_draft
🎉🎂 🎈🎊
11. React on ES6+ @nikgraf
Block-scoped binding constructs
const hello = 'Hello';
hello = 'Hola'; // this is not valid
12. React on ES6+ @nikgraf
Block-scoped binding constructs
function varTest() {
var x = 31;
if (true) {
var x = 71; // same variable!
console.log(x); // 71
}
console.log(x); // 71
}
13. React on ES6+ @nikgraf
Block-scoped binding constructs
function letTest() {
let x = 31;
if (true) {
let x = 71; // different variable
console.log(x); // 71
}
console.log(x); // 31
}
14. React on ES6+ @nikgraf
Classes
// The ES5 way
var Photo = React.createClass({
handleDoubleTap: function(e) { … },
render: function() { … },
});
// The ES6+ way
class Photo extends React.Component {
handleDoubleTap(e) { … }
render() { … }
}
15. React on ES6+ @nikgraf
Classes
// The ES5 way
var EmbedModal = React.createClass({
componentWillMount: function() { … },
});
// The ES6+ way
class EmbedModal extends React.Component {
constructor(props) {
super(props);
}
}
16. React on ES6+ @nikgraf
Property Initialisers
// The ES5 way
var Video = React.createClass({
getDefaultProps: function() {
return {
autoPlay: false,
maxLoops: 10,
};
},
getInitialState: function() {
return {
loopsRemaining: this.props.maxLoops,
};
},
propTypes: {
autoPlay: React.PropTypes.bool.isRequired,
maxLoops: React.PropTypes.number.isRequired
},
});
17. React on ES6+ @nikgraf
Property Initialisers
// The ES6+ way
class Video extends React.Component {
static defaultProps = {
autoPlay: false,
maxLoops: 10,
}
static propTypes = {
autoPlay: React.PropTypes.bool.isRequired,
maxLoops: React.PropTypes.number.isRequired
}
state = {
loopsRemaining: this.props.maxLoops,
}
}
20. React on ES6+ @nikgraf
Template Strings
// Multiline strings
const text = `In ES5 this is
not legal.
Good to know.`;
// Interpolate variable bindings
const city = 'Vienna';
const time = 'today';
// ES5
console.log('Hello ' + city + ', how are you ' + time + '?');
// ES6+
console.log(`Hello ${city}, how are you ${time}?`);
// results in "Hello Vienna, how are you today?"
25. React on ES6+ @nikgraf
ProTip: Eslint
created by Nicholas Zakas
- Enable/Disabled Rules on demand
- Follows the standard + JSX Support
- AirBnB’s fantastic JavaScript Guide + .eslintrc
https://github.com/airbnb/javascript/
27. React on ES6+ @nikgraf
End
Special thanks to Steven Luscher for the original post
on “React on ES6+”
https://babeljs.io/blog/2015/06/07/react-on-es6-plus
Checkout Belle
https://nikgraf.github.io/belle/