React Base Fiddle (JSX)
Starting point for creating JSFiddles with React. This uses React with Addons.
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react-with-addons.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/immutable/3.7.4/immutable.min.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
JavaScript 1.7
var store = Immutable.fromJS({
'buy-milk': { id: 'buy-milk', text: 'Buy milk', completed: true },
'finish-app': { id: 'finish-app', text: 'Finish the app', completed: false },
});
var globalContext = { hello: 'world' };
var TodoItem = React.createClass({
contextTypes: {
globalContext: React.PropTypes.object,
},
render: function() {
return <li onClick={this._clickHandler}>({this.props.todo.get('text')})</li>;
},
_clickHandler: function() {
// this gives an error because context was not passed correctly
console.log('globalContext', this.context.globalContext);
},
});
const TodoApp = React.createClass({
propTypes: {
store: React.PropTypes.object,
},
childContextTypes: {
globalContext: React.PropTypes.object,
},
getChildContext() {
return { globalContext: globalContext };
},
render() {
return (
<ul>
<TodoItem key="buy-milk" todo={this.props.store.get("buy-milk")} />
<TodoItem key="finish-app" todo={this.props.store.get("finish-app")} />
</ul>
);
},
});
React.render(<TodoApp store={store} />, document.getElementById('container'));