useReducer

by Artem

JavaScript

'use strict';

const useReducer = (reducer, initialValue) => {
	let state = {
  	value: initialValue
  };
  const dispatch = action => {
		state.value = reducer(state.value, action);
  };
  
  return [state, dispatch];
};

const [state, dispatch] = useReducer((state, action) => {
	if (action.op === '+') {
  	return state + action.value;
  }

	return state;
}, 10);

dispatch({ op: '+', value: 10 });

console.log(state.value);