Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data driven rendering

Marc Tamlyn
February 17, 2016

Data driven rendering

An introduction to ReactJS given at JSOxford

Marc Tamlyn

February 17, 2016
Tweet

More Decks by Marc Tamlyn

Other Decks in Technology

Transcript

  1. What is a Component? var Photo = React.createClass({ render: function()

    { return React.createElement('img', { src: this.props.imageSrc, }); } });
  2. Components with children var Photos = React.createClass({ render: function() {

    var photos = this.props.photos.map((photo) => { return React.createElement(Photo, { imageSrc: photo.imageSrc, }); }); return React.createElement('div', {}, photos); } });
  3. JSX var Photos = React.createClass({ render: function() { var photos

    = this.props.photos.map((photo) => { return <Photo imageSrc={photo.imageSrc} /> }); return <div>{photos}</div>; } });
  4. Virtual DOM •Renders in memory-light virtual DOM •Compares changes between

    trees •Flushes only changes to the document •Debounces rendering
  5. Data flow •Components have state as well as props •Minimise

    use of state •Handle at top level and pass functions around •Handle externally altogether!
  6. Data flow var Photos = React.createClass({ getInitialState: function() { return

    {photos: this.props.photos}; }, onLove: function() { … }, render: function() { var photos = this.state.photos.map((photo) => { return <Photo imageSrc={photo.imageSrc} onLove={this.onLove} /> }); return <div>{photos}</div>; } });
  7. Data flow var Photo = React.createClass({ render: function() { return

    <div> <img src={this.props.imageSrc} /> <a onclick={this.props.onLove}>Love</a> </div>; } });