Created
May 29, 2018 13:30
-
-
Save mindfulme/5b2f2383b4bbba6dbc16ee5824c6c61b to your computer and use it in GitHub Desktop.
Redux basics cheetsheet 1-4
This file contains 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 React, { Component } from 'react'; | |
import { combineReducers, createStore } from 'redux'; | |
const userReducer = (state={}, action) => { | |
// const newState = {...state} | |
switch(action.type) { | |
case "CHANGE_NAME": { | |
state = {...state, name: action.payload} | |
break; | |
} | |
case "CHANGE_AGE": { | |
state = {...state, age: action.payload} | |
break; | |
} | |
} | |
return state; | |
}; | |
const tweetsReducer = (state=[], action) => { | |
return state; | |
}; | |
const reducers = combineReducers({ | |
user:userReducer, | |
tweets:tweetsReducer, | |
}); | |
const reducer = function(state, action) { | |
if (action.type==='INC') { | |
return ++state+1; | |
} | |
else if (action.type==='DEC') { | |
return --state-action.payload; | |
} | |
return state; | |
} | |
const store = createStore(reducers); | |
store.subscribe(()=>{ | |
console.log('store changed', store.getState()); | |
}) | |
store.dispatch({type: 'CHANGE_AGE', payload: 33}) | |
store.dispatch({type: 'CHANGE_NAME', payload: "Will Rock"}) | |
class App extends Component { | |
render() { | |
return ( | |
<div> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment