Skip to content

Instantly share code, notes, and snippets.

@a-eid
Created March 30, 2017 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-eid/8b34df0139fded4cd5eb30bc98746d40 to your computer and use it in GitHub Desktop.
Save a-eid/8b34df0139fded4cd5eb30bc98746d40 to your computer and use it in GitHub Desktop.
simple counter example ....
import React from 'react';
import {render} from 'react-dom';
import {createStore} from 'redux'
const reducer = (state = 0 , action) =>
(action.type == 'inc' || action.type == 'dec') ? ((action.type == 'inc' ? state + 1 : state - 1) ) : state
const store = createStore(reducer)
const Counter = ({value , onInc , onDec}) => (
<div>
<h1>{value}</h1>
<button onClick={onInc}>+</button>
<button onClick={onDec}>-</button>
</div>
)
const init = ()=> {
render(<Counter value={store.getState()} onInc={()=> store.dispatch({type: 'inc'})} onDec={()=> store.dispatch({type: 'dec'})}/> , document.getElementById('root'))
}
init()
store.subscribe(init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment