Created
June 16, 2017 21:51
-
-
Save rogeriochaves/dc0854f64c76ee56ed5da91bd907685e to your computer and use it in GitHub Desktop.
Redux implementation, basically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const React = require("react"); | |
const ReactDOM = require("react-dom"); | |
const reducer = (state, action) => { | |
switch (action) { | |
case "INCREMENT": | |
return state + 1; | |
case "DECREMENT": | |
return state - 1; | |
default: | |
return state; | |
} | |
}; | |
const Counter = counter => | |
<div> | |
<button onClick={dispatch("DECREMENT")}>-</button> | |
<h1>{counter}</h1> | |
<button onClick={dispatch("INCREMENT")}>+</button> | |
</div>; | |
const Main = props => Counter(props.state); | |
let actions = []; | |
const initialState = 0; | |
const dispatch = action => () => { | |
actions.push(action); | |
const state = actions.reduce(reducer, initialState); | |
ReactDOM.render(<Main state={state} />, document.getElementById("root")); | |
}; | |
dispatch(null)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment