Skip to content

Instantly share code, notes, and snippets.

View ShMcK's full-sized avatar

Shawn McKay ShMcK

View GitHub Profile
@ShMcK
ShMcK / RealWorldRedux.js
Created February 26, 2018 15:28
Example of Redux Setup Complexity
import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import api from '../middleware/api'
import rootReducer from '../reducers'
import DevTools from '../containers/DevTools'
const store = (preloadedState) => {
return createStore(
rootReducer,
preloadedState,
@ShMcK
ShMcK / ReduxApiSimple.js
Created February 26, 2018 15:31
Simplified Redux API
const store = new Redux.Store({
initialState: {},
reducers: { count },
middlewares: [api, devTools],
})
@ShMcK
ShMcK / actionCreators
Created February 26, 2018 15:33
Action Creator Example
const increment = (count) => ({ type: 'INCREMENT', payload: count })
const decrement = (count) => ({ type: 'DECREMENT', payload: count })
@ShMcK
ShMcK / reducer.js
Created February 26, 2018 15:34
Reducer Example
const countReducer = (state, action) => {
switch(action.type) {
case INCREMENT:
return state + action.payload
case DECREMENT:
return state - action.payload
default:
return state
}
}
@ShMcK
ShMcK / simple-reducer.js
Created February 26, 2018 15:35
A simplified reducer
const countReducer = {
INCREMENT: (state, action) => state + action.payload,
DECREMENT: (state, action) => state - action.payload,
}
@ShMcK
ShMcK / thunk.js
Created February 26, 2018 15:37
thunk example
const incrementAsync = (count) => async (dispatch) => {
await delay()
dispatch(increment(count))
}
@ShMcK
ShMcK / thunkless-async.js
Created February 26, 2018 15:38
thunkless async
const incrementAsync = async (count) => {
await delay()
dispatch(increment(count))
}
@ShMcK
ShMcK / redux-grouping.js
Created February 26, 2018 15:39
Redux grouping
const count = {
state: 0,
reducers: {
increment: (state, action) => state + action.payload,
decrement: (state, action) => state - action.payload,
}
}
@ShMcK
ShMcK / action-creator.js
Last active February 26, 2018 15:44
Consistent Action Creator
const anyActionCreator = (namespace, type) => (payload) => ({
type: `${namespace}/${type}`,
payload
})
@ShMcK
ShMcK / rematch-outline.js
Last active February 26, 2018 15:51
Rematch outline
import { init, dispatch } from '@rematch/core'
import delay from './makeMeWait.js'
const count = {
state: 0,
reducers: {
increment: (state, payload) => state + payload,
decrement: (state, payload) => state - payload,
},
effects: {