From 2e07307f6190ee711729afdf0985de9306a2390d Mon Sep 17 00:00:00 2001 From: Nimmi Weeraddana Date: Tue, 14 Mar 2017 01:22:49 +0530 Subject: [PATCH] tutorial #4 was followed --- 1-basic-react/src/js/client.js | 56 +++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/1-basic-react/src/js/client.js b/1-basic-react/src/js/client.js index b08c8592..38793dde 100644 --- a/1-basic-react/src/js/client.js +++ b/1-basic-react/src/js/client.js @@ -6,25 +6,51 @@ import Layout from "./components/Layout"; const app = document.getElementById('app'); ReactDOM.render(, 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}) \ No newline at end of file +store.dispatch({type: 'CHANGE_NAME', payload:'Nimmi'}) //run through reducers +store.dispatch({type: 'CHANGE_AGE', payload:23}) +store.dispatch({type: 'CHANGE_AGE', payload:24}) \ No newline at end of file