const { createStore } = require('redux');
const store = createStore((state={count:0},action) => {
switch(action.type){
case 'INCREMENT':
return {
count: state.count + action.incrementBy
}
case 'DECREMENT':
return {
count: state.count - 1
}
case 'SET':
return {
count: action.count
}
case 'RESET':
return {
count:0
}
default:
return state;
}
});
const incrementBy = (counter = {incrementBy: 1}) => ({
type:'INCREMENT',
incrementBy: typeof counter.incrementBy === 'number' ? counter.incrementBy : 1
});
store.subscribe(incrementBy({incrementBy: 5}));
store.dispatch(incrementBy());
store.dispatch({
type : 'DECREMENT'
});
store.dispatch({
type :'SET',
count:101
});
store.dispatch({
type : 'RESET'
});