Skip to content

Instantly share code, notes, and snippets.

@cassiocardoso
Created February 13, 2018 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassiocardoso/b4a08e102bb9ad54f651ec49e49aa9e3 to your computer and use it in GitHub Desktop.
Save cassiocardoso/b4a08e102bb9ad54f651ec49e49aa9e3 to your computer and use it in GitHub Desktop.
Redux 101
import { createStore } from 'redux'
/**
* This is a reducer, a pure function with (state, action) => state signature.
* It describes how an action transforms the state into the next state.
*
*/
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
// Create the store
let store = createStore(counter);
/**
* Actions
*
*/
store.dispatch({ type: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment