Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created January 31, 2020 16:38
Show Gist options
  • Save azamsharp/32bf30bcd19f40e25ad7f2101f70e79c to your computer and use it in GitHub Desktop.
Save azamsharp/32bf30bcd19f40e25ad7f2101f70e79c to your computer and use it in GitHub Desktop.
const redux = require('redux')
const createStore = redux.createStore // returns a function which allows to create a global store
console.log(createStore)
const initialState = {
count: 0,
isAuthenticated: false
}
// state is global state
const reducer = (state = initialState, action) => {
if(action.type == 'INCRE_COUNTER') {
return {
...state,
count: state.count + 1
}
} else if(action.type == 'ADD_COUNTER') {
return {
...state,
count: state.count + action.value
}
}
return state
}
const store = createStore(reducer) // create a store
store.subscribe(() => {
console.log('[STORE UPDATED]')
})
console.log(store.getState())
store.dispatch({type: 'INCRE_COUNTER'})
console.log("AFTER UPDATING GLOBAL STATE")
console.log(store.getState())
// add 100 to counter
store.dispatch({type: 'ADD_COUNTER', value: 100})
console.log(store.getState())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment