On Github TimBuckley / react-native-presentation
Created by Tim Buckley
// ES5
function (x, y) {
return x + y
}
// ES6
(x, y) => x + y
// Example
var bozoItems = items.filter(item => item.is_bozo)
// Can also use multiline.
var sum = (x, y) => {
console.log('The sum is...')
return x + y
}
// ES6
function Person(){
this.age = 0
// var self = this // No longer necessary.
setInterval(() => {
this.age++ // |this| properly refers to the person object
}, 1000)
}
var p = new Person()
let a = 1
a = 2 // OK
const b = 1
b = 2 // -> ERROR
const nums = [1, 2, 3, 4]
nums.push(5) // OK. Variable can be modified, just not reassigned.
function foo() {
if (true) {
const myvar = 1
console.log(myvar) // prints 1
}
try {
console.log(myvar)
} catch(ex) {
console.log('Cannot access myvar outside its block')
}
}
foo()
// ES5
function favoriteThings(pie, num) {
pie = pie || 'apple'
num = num || 7
return pie + num
}
favoriteThings() // 'pie7'
// ES6
function favoriteThings(pie = 'apple', num = 7) {
return pie + num
}
favoriteThings() // 'pie7'
const myObj = {a: 1, b: 2, c: 3, d: 4}
const newObj = { ...myObj, d: 40, e: 50}
console.log(myObj) // {a: 1, b: 2, c: 3, d: 4}
console.log(newObj) // {a: 1, b: 2, c: 3, d: 40, e: 50}
const letters = ['b', 'c', 'd']
const newLetters = ['a', ...letters, 'e']
console.log(letters) // ['b', 'c', 'd']
console.log(newLetters) // ['a', 'b', 'c', 'd', 'e']
const numbers = [10, 20, 30, 40, 50]
const [first, ...middle, last] = numbers
console.log(first, last) // 10, 50
console.log(middle) // [20, 30, 40]
const position = {lat: 42, lng: 17, alt: 100}
const { lat, lng } = position
console.log(lat, lng) // 42, 17
const printLoc = ({lat, lng, alt}) => console.log(alt, lat, lng)
printLoc(position) // 100, 42, 17
// Importing
// ES5
var React = require('react')
var Text = React.Text
var View = React.View
// ES6
import React, { Text, View } from 'react'
// Exporting
// ES5
modules.export = Model
// ES6
export { myFunc, name2, ..., nameN }
export default Model
export default function (…) {…} // also class
export default function name1(…) { … } // also class
// ES5
function Hello(name) {
this.name = name
this.hello = function hello() {
return 'Hello' + this.name
}
}
// ES6
class Hello {
constructor(name) {
this.name = name
}
hello() {
return 'Hello ' + this.name
}
}
function* idMaker(){
let index = 0
while(index < 3) {
yield index++
}
return 'No more ids'
}
const genObj = idMaker()
genObj.next() // {done: false, value: 1}
genObj.next() // {done: false, value: 2}
genObj.next() // {done: false, value: 3}
genObj.next() // {done: true, value: 'No more ids'}
function* getRecentUsernames() {
const items = yield fetchRecentItems()
const authors = items.map(i => i.author)
const [ users, profiles ] = yield [
fetchUsers(authors),
fetchProfiles(authors),
]
return { users, profiles }
}
console.log(genWrapper(getRecentUsernames))
async function getRecentUsernames() {
const items = await fetchRecentItems()
const authors = items.map(i => i.author)
const [ users, profiles ] = await Promise.all([
fetchUsers(authors),
fetchProfiles(authors)
])
return { users, profiles }
}
console.log(getRecentUsernames())
Source: http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
Source: http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
(Demo)
{
newsfeed: {
items: [item1, item2, ...],
error: '',
loading: false
},
composeItem: {
body: '...',
nbhds: [],
},
global: {
user: {
username: 'user1'
}
},
(...)
}
{
type: 'SELECTED_ITEM',
itemId: 123
}
{
type: 'NEWSFEED_LOADED',
items: [item1, item2, ...]
}
function itemsReducer(state = initialState, action = {}) {
switch (action.type) {
case 'FAV_ITEM':
const { itemId } = action
return {
...state,
items: state.items.map(i =>
i.id === itemId ? favItem(i) : i)
}
default:
return state
}
}