Skip to content

Commit

Permalink
Merge pull request #1 from NimmiW/tut4
Browse files Browse the repository at this point in the history
tutorial learncodeacademy#4 was followed
  • Loading branch information
NimmiW authored Mar 13, 2017
2 parents 53a09a6 + 2e07307 commit e92370c
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions 1-basic-react/src/js/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,51 @@ import Layout from "./components/Layout";
const app = document.getElementById('app');
ReactDOM.render(<Layout/>, app);*/

import { createStore } from "redux";

const reducer = function(state, action) {
if(action.type == 'INC'){
return state + action.payload;
}
if(action.type == 'DEC'){
return state - action.payload;
}
return state;
import { combineReducers, createStore } from "redux";

const userReducer = function(state = {}, action) {
switch(action.type){
case 'CHANGE_NAME':{
//state.name = action.payload; //mutate
state = {...state, name: action.payload} //override the name
break;
}
case 'CHANGE_AGE':{
//state.age = action.payload;
state = {...state, age: action.payload}
break;
}
}
return state;
}

const tweetsReducer = function(state = [], action){
return state;
}

const store = createStore(reducer, 0);
const reducers = combineReducers({
user: userReducer,
//when user reducer executes, that state will be the
//user object in the store
tweets: tweetsReducer
//when tweets reducer executes, that state will be the
//tweets array in the store
})

/*const store = createStore(reducers, {
user: {
name: 'nimmi',
age: '23'
},
tweets: []
});*/ // in this exampe we do not set default values

const store = createStore(reducers);

store.subscribe(() => {
console.log('store changed',store.getState())
})

store.dispatch({type: 'INC', payload:1}) //run through reducer
store.dispatch({type: 'DEC', payload:1})
store.dispatch({type: 'DEC', payload:100})
store.dispatch({type: 'INC', payload:1})
store.dispatch({type: 'CHANGE_NAME', payload:'Nimmi'}) //run through reducers
store.dispatch({type: 'CHANGE_AGE', payload:23})
store.dispatch({type: 'CHANGE_AGE', payload:24})

0 comments on commit e92370c

Please sign in to comment.