
A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES
The simplest component need only include a render() method
/** @jsx React.DOM */ var WelcomeBanner = React.createClass({ render: function() { return(<div>Welcome to our webapp!</div>);} }); React.render(<WelcomeBanner />, document.body);
Without the JSX compilation
var WelcomeBanner = React.createClass({
render: function() {
return React.DOM.div(null, "Welcome to our webapp!");
}
});
React.render(WelcomeBanner({}), document.body);
/** @jsx React.DOM */ var WelcomeBanner = React.createClass({ render: function() { return(<div>Welcome to our webapp { this.props.name }!</div>);} }); React.render(<WelcomeBanner name="Nick" />, document.body);
/** @jsx React.DOM */ var WelcomeBanner = React.createClass({ getInitialState: function() { return { times: 0 }; }, handleClick: function() { this.setState({ times: this.state.times + 1 }); }, render: function() { return( <section> <div>Welcome to our webapp { this.props.name }!</div> <div>Times clicked: { this.state.times }</div> <button onClick={ this.handleClick }>Add One</button> </section> ); } });React.render(<WelcomeBanner name="Nick" />, document.body);

/** @jsx React.DOM */ var WelcomeBanner = React.createClass({ render: function() { return(<section> <div>Welcome to our webapp { this.props.name }!</div> <Adder /> </section>); } }); var Adder = React.createClass({ // getInitialState, addOne methods render: function() { return( <article> <div>Current click count: { this.state.count }</div> <button onClick={this.addOne}>Add One</button> </article> ); } });React.render(<WelcomeBanner />, document.body);
var Adder = React.createClass({
propTypes: {
attr1: React.PropTypes.string,
attr2: React.PropTypes.string.isRequired
},
// Rest of React code (render())
});
var MyComp = React.createClass({
getDefaultProps: function() {
return {
attr1: 5,
attr2: true
};
},
// Rest of React code
});var MyComp = React.createClass({
render: function() {
this.transferPropsTo(<div>DIV gets the properties set on MyComp</div>);
}
});<MyGrid cols="4"> <Gravatar email={ this.userEmail } /> <Gravatar email={ this.theirEmail } /> <Gravatar email={ this.thatEmail } /> </MyGrid>
// MyGrid render function
render: function() {
var computedChildren = myCustomProcess(this.props.children);
return (
<div class="row">
{ computedChildren }
</div>
);
}
var MyComp = React.createClass({
getInitialState: function() {
// return initial state object
},
componentWillMount: function() {
// any setup logic we have
},
componentDidMount: function() {
// post mount logic, manipulating dom, etc..
}
});var LoggerMixin = {
componentWillMount: function() { console.log("Component mounting"); },
componentWillUnmount: function() { console.log("Component unmounting"); }
};
var MyComp = React.createClass({
mixins: [LoggerMixin],
componentWillMount: function() {
// Both lifecycle methods will be called
},
render: function() {
return(<div>Render something</div>);
}
});