Last active
May 21, 2016 10:15
-
-
Save bellbind/b744ec4b682da2c91c4624cc6e36d447 to your computer and use it in GitHub Desktop.
[react][redux]Pure ES6(JSX less) example
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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<script src="https://fb.me/react-15.0.2.js"></script> | |
<script src="https://fb.me/react-dom-15.0.2.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.js" | |
></script> | |
<script src="script.js"></script> | |
</head> | |
<body></body> | |
</html> |
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
"use strict"; | |
// state reducer of redux (action should not be value types: str, num, ...) | |
function counter(state=0, action) { | |
switch (action.type) { | |
case "+": return state + 1; | |
case "-": return state - 1; | |
default: return state; | |
} | |
} | |
// react custom component with ES6 class | |
class CounterView extends React.Component { | |
render() { | |
const {state, onAction} = this.props; | |
//const num = React.createElement("span", {}, state); | |
const num = React.DOM.span({}, state); | |
const plus = React.DOM.button({onClick() {onAction("+");}}, "+"); | |
const minus = React.DOM.button({onClick() {onAction("-");}}, "-"); | |
return React.DOM.div({}, num, plus, minus); | |
} | |
} | |
window.addEventListener("load", _ => { | |
const main = document.createElement("main"); | |
document.body.appendChild(main); | |
const store = Redux.createStore(counter); | |
function render() { | |
const props = { | |
state: store.getState(), | |
onAction(sym) {store.dispatch({type: sym});} | |
}; | |
//const view = React.createElement(CounterView, props); | |
const view = React.createFactory(CounterView)(props); | |
// update view | |
ReactDOM.render(view, main); | |
} | |
store.subscribe(render); | |
render(); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo: https://gist.githack.com/bellbind/b744ec4b682da2c91c4624cc6e36d447/raw/index.html