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

React: Thinking State First

Doug Neiner
October 23, 2015

React: Thinking State First

A high level introduction to key benefits in React.js that are also a hurdle or a mental shift to using React. This was prepared for an audience of front end developers with varying experience with JavaScript.

Lux Mail Example (from demo at end): http://bit.ly/luxmail
Isomorphic Hot Loader I showed: http://bit.ly/1LLUQNn
Isomorphic React lib I should have probably used: http://bit.ly/1Xp4EUV

Doug Neiner

October 23, 2015
Tweet

More Decks by Doug Neiner

Other Decks in Technology

Transcript

  1. Why React.js? • Cohesive Templates and Code • Enforced Predictable

    Behavior • Synthetic Event System • Performance • Can be integrated gradually
  2. Our Canvas • It can stretch to any size •

    It may behave or appear differently to
 different people • It can become a performance bottleneck when you mess with it • So… how do we dynamically modify this canvas?
  3. jQuery $.each( someArray, function ( item ) { $( "#cart-items"

    ).append( "<li class='cart-item'>" + item.title + "</li>" ); } );
  4. jQuery var $ul = $( "<ul/>" ); $.each( someArray, function

    ( item ) { $ul.append( $( "<li />", { "class": "cart-item", text: item.title } ) ); } ); $container.html( $ul );
  5. Templates <ul> <% _.each( items, function ( item ) {

    %> <li class="cart-item"><%- item.title %></li> <% } ) %> </ul>
  6. Templates • When do you render them? • How often?

    • How do you get data out 
 of the rendered DOM?
  7. The “Template” Part • Special attributes: data-bind, ng-repeat • Learning

    template syntax: {{#each}} • Templates maintained separately from the
 code that uses them…
  8. We strongly believe that components are the right way to

    separate concerns rather than "templates" and "display logic." We think that markup and the code that generates it are intimately tied together. Additionally, display logic is often very complex and using template languages to express it becomes cumbersome. – React Docs (emphasis added)
  9. We strongly believe that components are the right way to

    separate concerns rather than "templates" and "display logic." We think that markup and the code that generates it are intimately tied together. Additionally, display logic is often very complex and using template languages to express it becomes cumbersome. – React Docs (emphasis added)
  10. A React Component (JSX) React.createClass({ render: function() { return <div

    className="cart"> <span className="cart-count"> { this.props.items.length } </span> <CartItems items={ this.props.items } /> </div>; } })
  11. A React Component (JSX) React.createClass({ render: function() { return <div

    className="cart"> <span className="cart-count"> { this.props.items.length } </span> <CartItems items={ this.props.items } /> </div>; } })
  12. A React Component (JS) React.createClass({ render: function render() { return

    React.createElement( "div", { className: "cart" }, React.createElement( "span", { className: "cart-count" }, this.props.items.length ), React.createElement( CartItems, { items: this.props.items } ) ); } });
  13. The “Template” Part • Special attributes: key, ref • Learning

    template syntax: {} + JS • Templates maintained with the code that 
 uses them.
  14. Props in Action <NewsItem title="Welcome" content="..." /> var NewsItem =

    React.createClass({ render: function() { return ( <div className="newsItem"> <h2>{ this.props.title }</h2> <p>{ this.props.content }</p> </div> ); } });
  15. Props in Action <NewsItem title="Welcome" content="..." /> var NewsItem =

    React.createClass({ render: function() { return ( <div className="newsItem"> <h2>{ this.props.title }</h2> <p>{ this.props.content }</p> </div> ); } });
  16. State in Action var Menu = React.createClass( { getInitialState: function

    () { return { open: false }; }, toggleOpen: function() { this.setState({ open: !this.state.open }); }, ... render: function() { return <div onClick={ this.toggleOpen }> { this.state.open ? "Open" : "Closed" } </div>; } } );
  17. State in Action var Menu = React.createClass( { getInitialState: function

    () { return { open: false }; }, toggleOpen: function() { this.setState({ open: !this.state.open }); }, ... render: function() { return <div onClick={ this.toggleOpen }> { this.state.open ? "Open" : "Closed" } </div>; } } );
  18. State in Action var Menu = React.createClass( { getInitialState: function

    () { return { open: false }; }, toggleOpen: function() { this.setState({ open: !this.state.open }); }, ... render: function() { return <div onClick={ this.toggleOpen }> { this.state.open ? "Open" : "Closed" } </div>; } } );
  19. State in Action var Menu = React.createClass( { getInitialState: function

    () { return { open: false }; }, toggleOpen: function() { this.setState({ open: !this.state.open }); }, ... render: function() { return <div onClick={ this.toggleOpen }> { this.state.open ? "Open" : "Closed" } </div>; } } );
  20. Any time props or state change, the component will call

    render. 
 Every time. (Default Behavior)
  21. <NewsApp> <UnreadCount /> <NewsList> <NewsItem> Changes Flow Down When clicked,

    reduce 
 UnreadCount by 1 and remove my class
 "is-unread"
  22. <NewsApp> <UnreadCount /> <NewsList> <NewsItem> Changes Flow Down When clicked,

    reduce 
 UnreadCount by 1 and remove my class
 "is-unread" ◦
  23. Your Commitment • Give control of as little or as

    much of your page as you want. • A transpilation step (if you use JSX) • Some shims/polyfills if you support IE8
  24. Still Not Convinced? • Accessibility • Server side rendering •

    Other rendering engines • Touch support • React Developer Tools
  25. Have a Single Source of Truth • Not in the

    DOM, in JavaScript variables • Changes are made first to the variables, then the view is updated to reflect the change.
  26. Don’t Make Relative Changes • Don’t “add 1” or “subtract

    1” when adding or removing an item from a cart. • Adjust the array of cart items, and update the count with the new length of that array.
  27. Don’t Make Relative Changes • Don’t call toggleClass, hoping the

    correct class gets added or removed. • Store a true or false value in a variable. Invert the value when you want to toggle, and then call toggleClass( "my-class", myVar )
  28. Have a Single Owner • Never have two separate pieces

    of JavaScript modifying the same aspect of a DOM element (style, children, etc)