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

Rethinking the view using React.js

Rethinking the view using React.js

Talk given at RedDotRubyConf 2015

Prathamesh Sonpatki

June 04, 2015
Tweet

More Decks by Prathamesh Sonpatki

Other Decks in Programming

Transcript

  1. var Hello = React.createClass({ render: function() { return ( <div>

    Hello React World! </div> ); } }); React.render(<Hello />,document.body);
  2. var TimeLine = React.createClass({ render: function() { return ( <div>

    <TweetCounts tweets=“3062” following=“509”/> <Tweets tweets={[..]}/> <Trends location=“pune" list={[..]} /> </div> ); } });
  3. JSX

  4. JSX

  5. JSX

  6. JSX is like a healthy vegetable that tastes like decadent

    chocolate cake. You feel guilty, but it’s good. Eric Elliot
  7. Any sufficiently complicated templating language contains an ad hoc, informally-specified,

    bug-ridden, slow implementation of a turing complete programming language. - Jasim A Basheer
  8. <Tweet> <div> <h1> Updated the website with a list of

    the fringe events </h1> <p>@reddotrubyconf</p> </div> </Tweet>
  9. var Tweet = React.createClass({ propTypes: { author: React.PropTypes.string, content: React.PropTypes.string,

    retweetCount: React.PropTypes.number, favoritesCount: React.PropTypes.number } });
  10. var Posts = React.createClass({ getInitialState: function() { return ( {

    posts: [] } ); }, render: function() { var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
  11. var Posts = React.createClass({ loadPosts: function() { // fetch posts

    }, componentDidMount: function() { this.loadPosts(); }, render: function() { var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
  12. var Posts = React.createClass({ loadPosts: function() { // fetch posts

    this.setState({posts: fetchedPosts}); }, componentDidMount: function() { this.loadPosts(); }, render: function() { this.loadPosts(); var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
  13. loadPosts: function() { // fetch posts this.setState({posts: fetchedPosts}); }, componentDidMount:

    function() { this.loadPosts(); }, render: function() { var posts = this.state.posts.map(function(post) { return ( <Post author={post.author} content={post.content} /> ); }); } });
  14. var Hello = React.createClass({displayName: "Hello", render: function() { return (

    React.createElement("div", null, "Hello React World!" ) ) } });
  15. var Hello = React.createClass({displayName: "Hello", render: function() { return (

    React.createElement("div", null, "Hello React World!" ) ) } });
  16. # /app/assets/javascripts/components/posts.js.jsx var Post = React.createClass({ render () { return

    ( <tr> <td> {this.props.post.author} </td> <td> {this.props.post.content} </td> </tr> ); } });
  17. render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) {

    return ( <Post author={post.author} content={post.content} /> ); }); return ( <div> <h1> Posts </h1> <table className="table"> <thead> <tr> <th>Author</th> <th>Content</th> </tr> </thead> <tbody> {posts} </tbody> </table> </div> ); }
  18. render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) {

    return ( <Post author={post.author} content={post.content} /> ); }); return ( <div> <h1> Posts </h1> <table className="table"> <thead> <tr> <th>Author</th> <th>Content</th> </tr> </thead> <tbody> {posts} </tbody> </table> </div> ); }
  19. render: function() { setTimeout(this.loadPosts, 5000); var posts = this.state.posts.map(function(post) {

    return ( <Post author={post.author} content={post.content} /> ); }); return ( <div> <h1> Posts </h1> <table className="table"> <thead> <tr> <th>Author</th> <th>Content</th> </tr> </thead> <tbody> {posts} </tbody> </table> </div> ); }
  20. <% @posts.each do |post| %> <%= react_component 'Post', { post:

    post }, { prerender: true, tag: ‘tr' } %> <% end %>
  21. <% @posts.each do |post| %> <%= react_component 'Post', { post:

    post }, { prerender: true, tag: ‘tr' } %> <% end %>
  22. Caveats • No access to document • All the dependencies

    must be specified in components.js • Components must be in global namespace
  23. var Tweet = React.createClass({ propTypes: { content: React.PropTypes.string, favCount: React.PropTypes.node

    }, render: function() { return ( <div> <div>Content: {this.props.content}</div> <div>Fav Count: {this.props.fav_count}</div> </div> ); } });