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

React Behind The Scenes

React Behind The Scenes

React tem se tornado uma das mais populares e queridas bibliotecas de JavaScript do mercado. O seu modo de construção usando componentes tem sido largamente adotado pela comunidade de desenvolvimento. Mas você sabe quais são os reais benefícios de utilizar React? Vamos falar de Virtual DOM, States, LifeCycles e como preparar seus componentes para um melhor funcionamento.

Thiago Alves Luiz

May 09, 2016
Tweet

More Decks by Thiago Alves Luiz

Other Decks in Programming

Transcript

  1. 50K

  2. !

  3. !

  4. jQuery var $myList = $('#my-list'); $.ajax({ url: 'https://api.domain.com/some-list', success: function

    (resp) { var data = resp.data; data.map(function (value) { var $myListItem = $('<li>').html(value.title); $myList.append($myListItem); }); } });
  5. React · Editor de texto · terminal · npm ·

    webpack · tasks runners · react
  6. HTML Resultante <!DOCTYPE html> ... <body> <ul id="my-list"> <li>Item 1</li>

    <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul> </body> </html>
  7. HTML <html> <head> <meta name="viewport" content="initial-scale=1"> <title>Critial Path: Hello world!</title>

    </head> <body> <p> Hello <span>web performance</span> students </p> <div><img src="dog.jpg"></div> </body> </html>
  8. CSS body { font-size: 16px; } p { font-weight: bold;

    } span { color: red; } img { float: right; } p > span { display: none; }
  9. var $body = $(body); var $p = $('.target'); $body.css({ backgroundColor:

    'red', borderColor: 'blue', color: 'blue' }); $p.css({ visibility: 'hidden' });
  10. var $body = $(body); $body.css({ padding: '20px', border: '2px solid

    blue', fontSize: '1.2rem' }); var $p = $('<p>').html('New node'); $body.append($p);
  11. this.setState({ foo: 'bar', test: 123, oData: [ { id: 1,

    title: 'Item 1'}, { id: 2, title: 'Item 2'}, { id: 3, title: 'Item 3'}, { id: 4, title: 'Item 4'}, ] });
  12. var App = React.createClass({ getInitialState: function () { return {

    counter: 0, data: [] }; }, handleClick: function (ev) { this.setState({ counter: this.state.counter + 1, data: this.data.push((new Date()).getTime()) }); }, render: function () { return ( <section> <Header title={ 'Update ' + this.state.counter } /> <List items={ this.state.data } /> <button onClick={ this.handleClick }>Refresh</button> </section> ); } });
  13. <App /> // Stateful Component <Header /> // Stateless Component

    <List /> // Stateless Component <button /> // Stateless Element
  14. var App = React.createClass({ ... render: function () { return

    ( <section> <Header title="Fixed title" /> <List items={ this.state.data } /> <button onClick={ this.handleClick }>Refresh</button> </section> ); } });