JSFiddle - React, Tailwind, and code Playground

by mojtaba

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.7.2/redux.js"></script>
click here

JavaScript

const counter = (state, action) => {
	console.log(state, action)
	if (typeof state === typeof undefined)
  	return 0
    
  if (action.type === 'increment')
  	return state + 1
    
  if (action.type === 'decrement')
  	return state - 1
    
  return state
}

const createStore = reducer => {
	let state
  let listener
  
  const getState = () => state
  
  const dispatch = action => {
  	state = reducer(state, action)
    
    if (typeof listener !== typeof undefined)
    	listener()
  }
  
  const subscribe = callback => {
  	listener = callback
  }
  
  dispatch({})
  return { getState, dispatch, subscribe }
}

const store = createStore(counter)

const render = () => {
	document.body.innerText = store.getState()
}

store.subscribe(render)
render()

document.addEventListener('click', e => {
  store.dispatch({ type: 'increment' })
})