Shorthand obj creation / desctructuring
const x = 2;
let two = 2;
console.log({ x, two });
const { something:
{ property: Alias }
, anything
} = bigPileOfMess;
Arrow FN
var flag = false;
const object = {
flag: true,
bound: () => console.log(this.flag),
unbound: function() { console.log(this.flag); }
}
const cb1 = object.bound;
const cb2 = object.unbound;
object.bound();
object.unbound();
setTimeout(cb1, 1);
setTimeout(cb2, 1);
Classes
class Base {
constructor() {
this.anotherFlag = true;
} }
const WithInspect = (klass) =>
class extends klass {
inspect() { console.log(JSON.stringify(this)); } }
class A extends WithInspect(Base) {
constructor(...args) {
super(...args);
this.flag = true;
} }
(new A).inspect();
What is it?
- a javascript library for building user interfaces
- opensourced: May 2013
- React is V in MVC
Key Features
đ
- Component Based
- Simple & Declarative
- Virtual DOM
- Enforcing One-way Dataflow
Components
â
- Not web-components
- Not ng directives
Your app as Component tree
đ˛
App
âââ Header
â âââ h1
âââ List
âââ ul
âââ li
âââ li
âââ li
Single component
- can have its own state
- can have props passed from parent component
- whenever state or props changes -> rerender triggered
Props (~attrs)
- to access, use
this.props
to set no way to set those - enforces one way data flow
Component Lifecycle
đļ â đą â đŧ
- componentWillMount()
- componentDidMount()
- componentWillReceiveProps()
- shouldComponentUpdate()
- componentWillUpdate()
- componentDidUpdate()
- componentWillUnmount()
Why 'rerender everything' is effective?
- VDOM
- shouldComponentUpdate
- extra benefits of immutable data
Authoring Component (JSX) â
class App extends React.Component {
constructor(...args) {
super(...args);
this.handleIncrease = this.handleIncrease.bind(this);
this.state = { count: 0 }
}
handleIncrease() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (
<div>
<h1>Count is {this.state.count}</h1>
<Counter onIncrease={this.handleIncrease} />
</div>
)
} }
Declarative / FP-ish
đ(âšī¸) = đ
PropTypes đŽ
import React, { Component, PropTypes } from 'react'
class App extends Component {
static propTypes = {
sum: PropTypes.number
}
}