This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const x = { a: 1 } | |
x.a = 2 // legit! the object itself is mutable | |
x.b = 3 // legit! the object itself is mutable | |
// x = { a: 2, b: 3 } | |
const a = [1] | |
a.pop() // legit! the array itself is mutable | |
a.push(2) // legit! the array itself is mutable | |
// a = [2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const optionalParam = (maybe) => { | |
// when falsey values aren't allowed, this will work | |
const definitely = maybe || 'oasis' | |
// ... | |
} | |
const optionalOption = (options) => { | |
// defaults in destructuring | |
const { definitely='oasis' } = options |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function helper(input) { | |
// ...do some complicated logic | |
return 'some result of the complicated logic' + input | |
} | |
const delegator = (arg) => { | |
// just move the complicated decision logic to a helper function | |
const definitely = helper(arg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const neverGonnaModifyYou = Object.freeze({ a: 3 }) | |
neverGonnaModifyYou.a = 4 // Won't throw, at least on node, chrome, ff & safari, but will leave the value intact | |
neverGonnaModifyYou.b = 4 // Won't throw, at least on node, chrome, ff & safari, but will leave the value intact | |
// neverGonnaModifyYou === { a: 3 } | |
const neverGonnaLetYouPop = Object.freeze([1]) | |
neverGonnaLetYouPop.pop() // throws! | |
neverGonnaLetYouPop.push(2) // throws! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createSelector } = require('reselect') | |
const { every } = require('lodash') | |
function greaterThan2(x) { | |
return x > 2 | |
} | |
function even(x) { | |
return x % 2 === 0 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from 'redux' | |
import { createAction, handleActions } from 'redux-actions' | |
const listFetched = createAction('ITEM_LIST_FETCHED') | |
const itemReducer = handleActions({ | |
ITEM_LIST_FETCHED: (state, action) => { | |
// the payload of the action is just a list of the items, | |
// each of which may have an id | |
return action.payload |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { find } from 'lodash' | |
import React, { Component } from 'react' | |
import { connect } from 'react-redux' | |
class WetComponent extends Component { | |
// ... do something with an item | |
} | |
const mapStateToProps = (state, ownProps) => { | |
// assume we're using redux-router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { find } from 'lodash' | |
import { combineReducers } from 'redux' | |
import { createAction, handleActions } from 'redux-actions' | |
const listFetched = createAction('ITEM_LIST_FETCHED') | |
const itemReducer = handleActions({ | |
ITEM_LIST_FETCHED: (state, action) => { | |
// the payload of the action is just a list of the items, | |
// each of which may have an id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { find } from 'lodash' | |
import { combineReducers } from 'redux' | |
import { createAction, handleActions } from 'redux-actions' | |
const listFetched = createAction('ITEM_LIST_FETCHED') | |
const itemReducer = handleActions({ | |
ITEM_LIST_FETCHED: (state, action) => { | |
// the payload of the action is just a list of the items, | |
// each of which has an id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from 'redux' | |
import { createAction, handleActions } from 'redux-actions' | |
const authorFetched = createAction('AUTHOR_FETCHED') | |
const bookFetched = createAction('BOOK_FETCHED') | |
const literaryReducer = handleActions({ | |
// maybe an author has a bunch of books as sub-resources | |
AUTHOR_FETCHED: (state, action) => { | |
const author = action.payload |