/** @jsx React.DOM */
// This is more complex example that uses two components -
// a service chooser form, and the individual services inside it.
var ServiceChooser = React.createClass({
getInitialState: function(){
return { total: 0 };
},
addTotal: function( price ){
this.setState( { total: this.state.total + price } );
},
render: function() {
var self = this;
var services = this.props.items.map(function(s){
// Create a new Service component for each item in the items array.
// Notice that I pass the self.addTotal function to the component.
return <Service name={s.name} price={s.price} active={s.active} addTotal={self.addTotal} />;
});
return <div>
<h1>Our services</h1>
<div id="services">
{services}
<p id="total">Total <b>${this.state.total.toFixed(2)}</b></p>
</div>
</div>;
}
});
var Service = React.createClass({
getInitialState: function(){
return { active: false };
},
clickHandler: function (){
var active = !this.state.active;
this.setState({ active: active });
// Notify the ServiceChooser, by calling its addTotal method
this.props.addTotal( active ? this.props.price : -this.props.price );
},
render: function(){
return <p className={ this.state.active ? 'active' : '' } onClick={this.clickHandler}>
{this.props.name} <b>${this.props.price.toFixed(2)}</b>
</p>;
}
});
var services = [
{ name: 'Web Development', price: 300 },
{ name: 'Design', price: 400 },
{ name: 'Integration', price: 250 },
{ name: 'Training', price: 220 }
];
// Render the ServiceChooser component, and pass the array of services
React.renderComponent(
<ServiceChooser items={...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.