Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active May 21, 2016 10:15
Show Gist options
  • Save bellbind/b744ec4b682da2c91c4624cc6e36d447 to your computer and use it in GitHub Desktop.
Save bellbind/b744ec4b682da2c91c4624cc6e36d447 to your computer and use it in GitHub Desktop.
[react][redux]Pure ES6(JSX less) example
<!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>
"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