Skip to content

Instantly share code, notes, and snippets.

@velopert
Created September 4, 2016 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velopert/669ace5d73e6ed2eb534b40c0618d238 to your computer and use it in GitHub Desktop.
Save velopert/669ace5d73e6ed2eb534b40c0618d238 to your computer and use it in GitHub Desktop.
[React.js] 5-6편: Redux Reducer

reducers/counter.js

import * as types from '../actions/ActionTypes';

const initialState = {
    number: 0,
    dummy: 'dumbdumb',
    dumbObject: {
        d: 0,
        u: 1,
        m: 2,
        b: 3
    }
};

export default function counter(state = initialState, action) {
    /* ... */
    switch(action.type) {
        case types.INCREMENT:
            return {
                ...state,
                number: state.number + 1,
                dumbObject: { ...state.dumbObject, u: 0 }
            };
        case types.DECREMENT: 
            return {
                ...state,
                number: state.number - 1
            };
        default:
            return state;
    }
}

reducers/ui.js

import * as types from '../actions/ActionTypes';

const initialState = {
    color: [255, 255, 255]
};

export default function ui(state = initialState, action) {
    if(action.type === types.SET_COLOR) {
        return {
            color: action.color
        };
    } else {
        return state;
    }
}

reducers/index.js

import { combineReducers } from 'redux';
import counter from './counter';
import ui from './ui';


const reducers = combineReducers({
    counter, ui
});

export default reducers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment