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

Why React is Awesome ・ #BCBK 6

Why React is Awesome ・ #BCBK 6

My talk about React at BarCamp Bangkhen 2015

Thai Pangsakulyanont

November 22, 2015
Tweet

More Decks by Thai Pangsakulyanont

Other Decks in Programming

Transcript

  1. Slide № 2 About Me Thai Pangsakulyanont (@dtinth), a frontend

    enthusiast.
 My Blog: http://me.dt.in.th/
 My Toys: http://spacet.me/ Working at Taskworld as web engineer.
 We use React and Node.js. Building a free web-based rhythm game, Bemuse.
 Please check it out at https://bemuse.ninja/
 The front-end part uses React.
  2. Slide № 3 Note: Since I used Angular.js a lot,

    there will be many comparisons with Angular.
  3. Slide № 4 Note: This session aims at showing the

    awesomeness of React. Therefore, many details are left out.
  4. Slide № 8 let todos = [ ] let nextId

    = 1 function addTodo (text) { let id = nextId++ }
  5. Slide № 9 let todos = [ ] let nextId

    = 1 function addTodo (text) { let id = nextId++ todos.push({ id: id, text: text }) }
  6. Slide № 13 let todos = [ ] let nextId

    = 1 function addTodo (text) { let id = nextId++ todos.push({ id: id, text: text }) }
  7. Slide № 14 let todos = [ ] let nextId

    = 1 function addTodo (text) { let id = nextId++ todos.push({ id: id, text: text }) $('<li></li>') .text(text).attr('id', 'todo' + id) .appendTo('#todos') }
  8. Slide № 16 function removeTodo (idToRemove) { todos = todos.filter(

    ({ id }) => id !== idToRemove ) $('#todo' + idToRemove).remove() }
  9. Slide № 17 function removeTodo (idToRemove) { todos = todos.filter(

    ({ id }) => id !== idToRemove ) $('#todo' + idToRemove).remove() } Simple and Straightforward
  10. Slide № 19 If we update DOM manually, our code

    would quickly become a spaghetti.
  11. Slide № 22 let todos = [ ] let nextId

    = 1 function addTodo (text) { let id = nextId++ todos.push({ id: id, text: text }) }
  12. Slide № 23 let todos = [ ] let nextId

    = 1 function addTodo (text) { let id = nextId++ todos.push({ id: id, text: text }) updateUI() }
  13. Slide № 25 function removeTodo (idToRemove) { todos = todos.filter(

    ({ id }) => id !== idToRemove ) updateUI() }
  14. Slide № 28 function updateUI () { $('#app').html(render(todos)) } function

    render (todos) { return ( '<ul>' + todos.map(todo => '<li>' + todo.text + '</li>' ).join('') + '</ul>' ) }
  15. Slide № 29 function updateUI () { $('#app').html(render(todos)) } function

    render (todos) { return ( '<ul>' + todos.map(todo => '<li>' + todo.text + '</li>' ).join('') + '</ul>' ) } Very simple! Predictable! Testable!
  16. Slide № 31 Each time updateUI() is called the browser

    throws away old UI, and renders the new one.
  17. Slide № 33 function updateUI () { $('#app').html(render(todos)) } function

    render (todos) { return ( '<ul>' + (todos.map(todo => '<li>' + todo.text + '</li>' )).join('') + '</ul>' ) }
  18. Slide № 34 function updateUI () { $('#app').html(render(todos)) } function

    render (todos) { return ( '<ul>' + (todos.map(todo => '<li>' + escapeHtml(todo.text) + '</li>' )).join('') + '</ul>' ) }
  19. Slide № 36 function render (todos) { return ( <ul>

    {todos.map(todo => <li>{todo.text}</li> )} </ul> ) }
  20. Slide № 37 JSX function render (todos) { return (

    <ul> {todos.map(todo => <li>{todo.text}</li> )} </ul> ) }
  21. Slide № 38 Plain JS function render (todos) { return

    ( React.createElement('ul', null, todos.map(todo => React.createElement('li', null, todo.text) ) ) ) }
  22. Slide № 39 function render (todos) { return ( React.createElement('ul',

    null, todos.map(todo => React.createElement('li', null, todo.text) ) ) ) } Returns a “virtual DOM”
  23. Slide № 41 function updateUI () { let view =

    render(todos) let container = document.querySelector('#app') }
  24. Slide № 42 function updateUI () { let view =

    render(todos) let container = document.querySelector('#app') ReactDOM.render(view, container) }
  25. Slide № 43 function updateUI () { let view =

    render(todos) let container = document.querySelector('#app') ReactDOM.render(view, container) } Blazing fast!
  26. Slide № 45 function render (todos) { return ( <ul>

    {todos.map(todo => <li>{todo.text}</li> )} </ul> ) }
  27. Slide № 46 function render (todos) { return ( <ul>

    {todos.map(todo => <li>{todo.text}</li> )} </ul> ) } JSX ReactDOM.render()
  28. Slide № 48 <div class="setting-section" ng-if="!self.display"> <div class="container"> <h2>Preview</h2> <div

    title="Click to save" sign-display="settings" ng-click="save()" ng-style="previewSize(setti class="preview-container"></div> </div> </div> <div class="setting-section" ng-if="!self.display"> <div class="container"> <h2>Appearance</h2> <div class="form-horizontal"> <div class="form-group"> <label class="control-label col-sm-3">Font</label> <div class="col-sm-4"> <input class="form-control" ng-model="settings.fontFamily"> </div> <div class="col-sm-2"> <button type="button" ng-class="{ btn: true, 'btn-default': true, 'btn-block': true, activ settings.fontBold }" ng-click="settings.fontBold = !settings.fontBold">Bold</button> </div> </div> <div class="form-group"> <label class="control-label col-sm-3">Color</label> <div class="col-sm-6"> <div class="row"> <div class="col-xs-5"> <input class="form-control" ng-model="settings.colorFg"> </div> <div class="col-xs-2 text-center text-muted"> <div class="form-control-static">on</div> </div> <div class="col-xs-5"> <input class="form-control" ng-model="settings.colorBg"> </div> </div> </div> </div> </div> </div> </div>
  29. Slide № 52 Step 1: break the UI into a

    component hierarchy https://facebook.github.io/react/docs/thinking-in-react.html
  30. Slide № 59 const Options = React.createClass({ render () {

    return <div className="Options"> </div> }
  31. Slide № 60 const Options = React.createClass({ render () {

    return <div className="Options"> <Panel title="Player Settings"> <OptionsPlayer onClose={this.props.onClose} /> </Panel> <div className="Optionsͷvgroup"> <Panel title="Input Settings"> <OptionsInput /> </Panel> <Panel title="Advanced Settings"> <OptionsAdvanced /> </Panel> </div> </div> }
  32. Slide № 61 const Options = React.createClass({ render () {

    return <div className="Options"> <Panel title="Player Settings"> <OptionsPlayer onClose={this.props.onClose} /> </Panel> <div className="Optionsͷvgroup"> <Panel title="Input Settings"> <OptionsInput /> </Panel> <Panel title="Advanced Settings"> <OptionsAdvanced /> </Panel> </div> </div> } Let’s look at <Panel>
  33. Slide № 63 const Panel = React.createClass({ render () {

    return ( <div className="Panel"> <div className="Panelͷtitle"> {this.props.title} </div> <div className="Panelͷcontent"> {this.props.children} </div> </div> ) } })
  34. Slide № 64 const Panel = React.createClass({ render () {

    return ( <div className="Panel"> <div className="Panelͷtitle"> {this.props.title} </div> <div className="Panelͷcontent"> {this.props.children} </div> </div> ) } })
  35. Slide № 65 const Panel = React.createClass({ render () {

    return ( <div className="Panel"> <div className="Panelͷtitle"> {this.props.title} </div> <div className="Panelͷcontent"> {this.props.children} </div> </div> ) } }) <Panel title="Hello"> <div>Hi!</div> </Panel>
  36. Slide № 66 const Panel = React.createClass({ render () {

    return ( <div className="Panel"> <div className="Panelͷtitle"> Hello </div> <div className="Panelͷcontent"> <div>Hi!</div> </div> </div> ) } }) <Panel title="Hello"> <div>Hi!</div> </Panel>
  37. Slide № 67 const Panel = React.createClass({ render () {

    return ( <div className="Panel"> <div className="Panelͷtitle"> {this.props.title} </div> <div className="Panelͷcontent"> {this.props.children} </div> </div> ) } }) Very reusable
  38. Slide № 74 React view model, framework, etc… Flux Redux

    Rx.JS Angular Backbone Meteor Bacon.js (none)
  39. Slide № 75 React view model, framework, etc… Flux Redux

    Rx.JS Angular Backbone Meteor Bacon.js (none)
  40. Slide № 76 React view model, framework, etc… Flux Redux

    Rx.JS Angular Backbone Meteor Bacon.js (none)
  41. Slide № 80 Data Virtual DOM Component Virtual DOM Real

    DOM ReactDOM.render() Does not require browser Requires browser
  42. Slide № 81 Data Virtual DOM Component Virtual DOM HTML

    String ReactDOMServer.renderToString() Does not require browser Does not require browser