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

Construindo interfaces componentizadas com React

Construindo interfaces componentizadas com React

Tweet

More Decks by Sérgio Rafael Siqueira

Other Decks in Programming

Transcript

  1. CTO e Co-fundador da INBEP, startup com foco em educação.

    Sérgio Rafael Siqueira github.com/sergiors sergiorsiqueira.com [email protected]
  2. <script id="entry-template" type="text/x-handlebars-template"> <p> {{#if liked}} You like this. Click

    to toggle. {{else}} You haven't liked this. Click to toggle. {{/if}} </p> </script>
  3. {{ mustache }} <p> {{#liked}} You like this. Click to

    toggle. {{^liked}} You haven't liked this. Click to toggle. {{/liked}} </p>
  4. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  5. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  6. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  7. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  8. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  9. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  10. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    false } }, handleClick: function() { this.setState({ liked: !this.state.liked }); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return React.DOM.p({onClick: this.handleClick}, 'You '+text+' this.'); } });
  11. JSX

  12. var LikeButton = React.createClass({ getInitialState: function() { return {liked: false}

    }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}> You {text} this. Click to toggle. </div> ); } });
  13. var LikeButton = React.createClass({ getInitialState: function() { return { liked:

    this.props.liked || false } }, componentWillMount: function() { Backbone.Events.on('change:anywhere', function(data) { this.setState({ liked: data.liked }); }.bind(this)); }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}>
  14. var LikeButton = React.createClass({ getInitialState: function() { return {liked: false}

    }, componentWillMount: function() { Backbone.Events.on('change:anywhere', function(data) { this.setState({ liked: data.liked }); }.bind(this)); }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}> You '+text+' this. Click to toggle. </div>
  15. var LikeButton = React.createClass({ getInitialState: function() { return {liked: false}

    }, componentDidMount: function() { $.ajax('example.php') .done(function(res) { this.setState({ liked: res.liked }); }.bind(this)); }, handleClick: function() { this.setState({liked: !this.state.liked}); }, render: function () { var text = this.state.liked ? 'like' : 'haven\'t liked'; return ( <div onClick={this.handleClick}> You '+text+' this. Click to toggle.
  16. componentDidMount Chamado imediatamente após o render inicial. A DOM já

    esta pronta! Ideal para trabalhar com biblioteca de terceiros que precisam do objeto na DOM.