Redux 기초

less than 1 minute read

Reducer와 Store, Action을 사용해보기

const reducer = (state = [], action) => {
  switch (action.type){
    case 'split_string':
      return action.payload.split('');
      
    case 'add_character':
      return [...state, action.payload]
      
      default:
      return state   
  }
};

const store = Redux.createStore(reducer);

store.getState() // []

const action = { 
  type: 'split_string',
  payload: 'asdf' 
};

store.dispatch(action);
store.getState() // ["a","s","d","f"]

const action2 = {
  type: 'add_character',
  payload: 'a'
}

store.dispatch(action2)
store.getState() // ["a","s","d","f","a"]