Last active
August 15, 2016 01:22
-
-
Save tzkmx/a388f4da6f09f991ab5007b48ef54f22 to your computer and use it in GitHub Desktop.
React-Redux standalone in browser (no browserify, no webpack, just plain <script> tags)
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
// React component | |
var Counter = React.createClass( { | |
render: function() { | |
var value = this.props.value; | |
var onIncreaseClick = this.props.onIncreaseClick; | |
return ( | |
React.createElement("div", null, | |
React.createElement("span", null, value), | |
React.createElement("button", {onClick: onIncreaseClick}, "Increase") | |
) | |
); | |
} | |
}); | |
// Action: | |
var increaseAction = {type: 'increase'}; | |
// Reducer: | |
function counter(state, action) { | |
if (typeof state === 'undefined') { | |
state = {count: 0}; | |
} | |
var count = state.count; | |
switch(action.type){ | |
case 'increase': | |
return {count: count+1}; | |
default: | |
return state; | |
} | |
} | |
// Store: | |
var store = Redux.createStore(counter); | |
// Map Redux state to component props | |
function mapStateToProps(state) { | |
return { | |
value: state.count | |
}; | |
} | |
// Map Redux actions to component props | |
function mapDispatchToProps(dispatch) { | |
return { | |
onIncreaseClick: function() { | |
dispatch(increaseAction) | |
} | |
}; | |
} | |
// Connected Component: | |
var App = connect( | |
mapStateToProps, | |
mapDispatchToProps | |
)(Counter); | |
ReactDOM.render( | |
React.createElement(Provider, {store: store}, | |
React.createElement(App, null) | |
), | |
document.getElementById('ui') | |
); |
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"> | |
<title>Standalone browser react + redux</title> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.0/react-dom.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/react-redux/4.4.5/react-redux.min.js"></script> | |
</head> | |
<body> | |
<div id="ui"></div> | |
<!-- example taken from https://github.com/jackielii/simplest-redux-example/blob/es5/index.js | |
fixed because does not call ReactDOM --> | |
<script type="text/javascript" src="js/counter-redux-example.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment