React Base Fiddle (JSX)
Starting point for creating JSFiddles with React. This uses React with Addons.
by Kpax Qin
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://fb.me/react-with-addons-15.0.1.js"></script>
<script src="https://fb.me/react-dom-15.0.1.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
JavaScript 1.7
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 10
}
}
onDecrement() {
this.setState({
value: this.state.value - 1
})
}
onIncrement() {
this.setState({
value: this.state.value + 1
})
}
render() {
const {value} = this.state;
return (
<div>
<button onClick={this.onIncrement.bind(this)}>+</button>
{value}
<button onClick={this.onDecrement.bind(this)}>-</button>
</div>
)
}
}
class CounterList extends React.Compoenent {
constructor(props) {
super(props)
this.state = {
uid: 0,
list: []
}
}
insertCounter() {
const {uid, list} = this.state
this.setState({
list: list.push(uid++)
})
}
removeCounter() {
const {list} = this.state
this.setState({
list: list.slice(0, list.length - 1)
})
}
render () {
return (
<div>
<button onClick={this.insertCounter.bind(this)}>Insert</button>
<button onClick={this.removeCounter.bind(this)}>Remove</button>
{this.state.list.map(i=> <Counter key={i}/>)}
<div/>
)
}
}
ReactDOM.render(
<CounterList />,
document.getElementById('container')
);