Skip to content

Instantly share code, notes, and snippets.

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]
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
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)
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!
const { createSelector } = require('reselect')
const { every } = require('lodash')
function greaterThan2(x) {
return x > 2
}
function even(x) {
return x % 2 === 0
}
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
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
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
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
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