Workshop Base Fiddle

by Ayité D'almeida

HTML

<script src="https://npmcdn.com/redux/dist/redux.js"></script>
<script src="https://npmcdn.com/react-redux/dist/react-redux.js"></script>
<script src="https://npmcdn.com/expect/umd/expect.js"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
<script src="&quot;https://npmcdn.com/react/dist/react.js&quot;"></script>
<script src="https://npmcdn.com/react-dom/dist/react-dom.js"></script>
<div id="root"></div>

Babel + JSX

/* Create a store from a reducer using `createStore`.
 * 
 * Dispatch an action from inside an click event listener using. (You can just attach the listener to `document`.)
 * 
 * Subscribe to state changes and render the current count by setting the `innerHTML` of the body.
 *
 * Bonus: reimplement `createStore`
 */
const { createStore } = Redux;

const counter = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
};

// Create a store from a reducer 
const store = createStore(counter);

// Log the initial state
console.log(store.getState());

// Dispatch an action from inside an click event
//store.dispatch({ type: 'INCREMENT' });
//console.log(store.getState());

// Subscribe to state changes and render the current count
const render = () => {
	document.body.innerHTML = store.getState();
};

store.subscribe(render);
render();


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

/*
store.subscribe(() => {
	document.body.innerHTML = store.getState();
});

document.addEventListener('click', () => {
	store.dispatch({ type: 'INCREMENT' });
});
*/


//const root = document.getElementById('root');