<script src="http://fb.me/react-0.10.0.min.js"></script>
React.createClass()
passing an object with properties and methods. Each component has a state (an object with data) and is responsible for its display — the render()
method is called when the state changes. Here is an example of building a regular timer: /** @jsx React.DOM */ // React.createClass. var TimerExample = React.createClass({ getInitialState: function(){ // render. // this.state, . return { elapsed: 0 }; }, componentDidMount: function(){ // componentDidMount react', // . : this.timer = setInterval(this.tick, 50); }, componentWillUnmount: function(){ // , // . : clearInterval(this.timer); }, tick: function(){ // 50. // . setState this.setState({elapsed: new Date() - this.props.start}); }, render: function() { var elapsed = Math.round(this.state.elapsed / 100); // dot (xx.x): var seconds = (elapsed / 10).toFixed(1); // <p> , react // , seconds. return <p>This example was started <b>{seconds} seconds</b> ago.</p>; } }); React.renderComponent( <TimerExample start={Date.now()} />, document.body );
full JSFiddle exampletick()
function is called to calculate the elapsed seconds. /** @jsx React.DOM */ var MenuExample = React.createClass({ getInitialState: function(){ return { focused: 0 }; }, clicked: function(index){ // // this.setState({focused: index}); }, render: function() { // items, // , var self = this; // map , // <li> . return ( <div> <ul>{ this.props.items.map(function(m, index){ var style = ''; if(self.state.focused == index){ style = 'focused'; } // bind(). // index clicked: return <li className={style} onClick={self.clicked.bind(self, index)}>{m}</li>; }) } </ul> <p>Selected: {this.props.items[this.state.focused]}</p> </div> ); } }); // , React.renderComponent( <MenuExample items={ ['Home', 'Services', 'About', 'Contact us'] } />, document.body );
full JSFiddle exampleReact.DOM.p
component. You can learn more about this at this link . /** @jsx React.DOM */ // var SearchExample = React.createClass({ getInitialState: function(){ return { searchString: '' }; }, handleChange: function(e){ // , . // , React', // . , this.state.searchString. this.setState({searchString:e.target.value}); }, render: function() { var libraries = this.props.items, searchString = this.state.searchString.trim().toLowerCase(); if(searchString.length > 0){ // . . libraries = libraries.filter(function(l){ return l.name.toLowerCase().match( searchString ); }); } return <div> <input type="text" value={this.state.searchString} onChange={this.handleChange} placeholder="Type here" /> <ul> { libraries.map(function(l){ return <li>{l.name} <a href={l.url}>{l.url}</a></li> }) } </ul> </div>; } }); var libraries = [ { name: 'Backbone.js', url: 'http://documentcloud.imtqy.com/backbone/'}, { name: 'AngularJS', url: 'https://angularjs.org/'}, { name: 'jQuery', url: 'http://jquery.com/'}, { name: 'Prototype', url: 'http://www.prototypejs.org/'}, { name: 'React', url: 'http://facebook.imtqy.com/react/'}, { name: 'Ember', url: 'http://emberjs.com/'}, { name: 'Knockout.js', url: 'http://knockoutjs.com/'}, { name: 'Dojo', url: 'http://dojotoolkit.org/'}, { name: 'Mootools', url: 'http://mootools.net/'}, { name: 'Underscore', url: 'http://documentcloud.imtqy.com/underscore/'}, { name: 'Lodash', url: 'http://lodash.com/'}, { name: 'Moment', url: 'http://momentjs.com/'}, { name: 'Express', url: 'http://expressjs.com/'}, { name: 'Koa', url: 'http://koajs.com/'}, ]; // SearchExample React.renderComponent( <SearchExample items={ libraries } />, document.body );
full JSFiddle example // , - // . 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){ // Service . // , self.addTotal function . 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 }); // ServiceChooser, addTotal 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 } ]; // ServiceChooser React.renderComponent( <ServiceChooser items={ services } />, document.body );
full JSFiddle example /** @jsx React.DOM */ // - // Instagram AJAX. var Picture = React.createClass({ // - // . clickHandler: function(){ // , onClick, // : this.props.onClick(this.props.ref); }, render: function(){ var cls = 'picture ' + (this.props.favorite ? 'favorite' : ''); return ( <div className={cls} onClick={this.clickHandler}> <img src={this.props.src} width="200" title={this.props.title} /> </div> ); } }); var PictureList = React.createClass({ getInitialState: function(){ // AJAX, // , : return { pictures: [], favorites: [] }; }, componentDidMount: function(){ // , jQuery AJAX var self = this; // API, var url = 'https://api.instagram.com/v1/media/popular?client_id=' + this.props.apiKey + '&callback=?'; $.getJSON(url, function(result){ if(!result || !result.data || !result.data.length){ return; } var pictures = result.data.map(function(p){ return { id: p.id, url: p.link, src: p.images.low_resolution.url, title: p.caption ? p.caption.text : '', favorite: false }; }); // . render. // , pictures // . self.setState({ pictures: pictures }); }); }, pictureClick: function(id){ // id ID . // pictures var favorites = this.state.favorites, pictures = this.state.pictures; for(var i = 0; i < pictures.length; i++){ // if(pictures[i].id == id) { if(pictures[i].favorite){ return this.favoriteClick(id); } // , // , : favorites.push(pictures[i]); pictures[i].favorite = true; break; } } // , this.setState({pictures: pictures, favorites: favorites}); }, favoriteClick: function(id){ // // , -. var favorites = this.state.favorites, pictures = this.state.pictures; for(var i = 0; i < favorites.length; i++){ if(favorites[i].id == id) break; } // favorites.splice(i, 1); for(i = 0; i < pictures.length; i++){ if(pictures[i].id == id) { pictures[i].favorite = false; break; } } // this.setState({pictures: pictures, favorites: favorites}); }, render: function() { var self = this; var pictures = this.state.pictures.map(function(p){ return <Picture ref={p.id} src={p.src} title={p.title} favorite={p.favorite} onClick={self.pictureClick} /> }); if(!pictures.length){ pictures = <p>Loading images..</p>; } var favorites = this.state.favorites.map(function(p){ return <Picture ref={p.id} src={p.src} title={p.title} favorite={true} onClick={self.favoriteClick} /> }); if(!favorites.length){ favorites = <p>Click an image to mark it as a favorite.</p>; } return ( <div> <h1>Popular Instagram pics</h1> <div className="pictures"> {pictures} </div> <h1>Your favorites</h1> <div className="favorites"> {favorites} </div> </div> ); } }); // PictureList body. // API Instagram . // , http://instagram.com/developer/ React.renderComponent( <PictureList apiKey="642176ece1e7445e99244cec26f4de1f" />, document.body );
full JSFiddle exampleSource: https://habr.com/ru/post/229629/
All Articles