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

React on Rails

React on Rails

React is a new framework javascript, developed by Facebook. And the description on site is ""Lots of people use React as the V in MVC. Since React makes no assumptions about the rest of your technology stack, it's easy to try it out on a small feature in an existing project"".
I'll talk about how implement this framework, on your workflow with RoR using a gem called react-rails.
The talk will contain this informations, States and Props, Virtual DOM and JSX about the framework. Using JSX, organization components, Pre-render, React unobtrusive and helpers about the gem.

Vinícius Palma

September 17, 2015
Tweet

More Decks by Vinícius Palma

Other Decks in Programming

Transcript

  1. #rubyconf2015 React - simple component var HelloMessage = React.createClass({ render:

    function() { return <div>Hello {this.props.name}</div>; } }); React.render(<HelloMessage name="John" />, mountNode);
  2. #rubyconf2015 React - stateful component var Timer = React.createClass({ getInitialState:

    function() { return {secondsElapsed: 0}; }, tick: function() { this.setState({secondsElapsed: this.state.secondsElapsed + 1}); }, componentDidMount: function() { this.interval = setInterval(this.tick, 1000); }, componentWillUnmount: function() { clearInterval(this.interval); }, render: function() { return ( <div>Seconds Elapsed: {this.state.secondsElapsed}</div> ); } }); React.render(<Timer />, mountNode);
  3. #rubyconf2015 Rails is a web-application framework that includes everything needed

    to create database-backed web applications according to the Model-View-Controller (MVC) pattern. Rails
  4. react-rails #rubyconf2015 Rendering and mounting <%= react_component('HelloMessage', name: 'John') %>

    <div data-react-class="HelloMessage" data-react-props="{&quot;name&quot;:&quot;John&quot;}"></div>
  5. react-rails #rubyconf2015 Server rendering <%= react_component('HelloMessage', {name: 'John'}, {prerender: true})

    %> <div data-react-class="HelloMessage" data-react-props=“{&quot;name&quot;:&quot;John&quot;}”> <h1>Hello, John!</h1> </div>
  6. #rubyconf2015 File with components and dependencies Default: components.js react-rails -

    Rendering and mounting Accessible in the global scope window.Component = Component Be careful, document jQuery and some other libs won't work in this environment :( *
  7. react-rails #rubyconf2015 var Post = React.createClass({ propTypes: { title: React.PropTypes.string,

    body: React.PropTypes.string, published: React.PropTypes.bool, publishedBy: React.PropTypes.instanceOf(Person) }, render: function() { return ( <div> <div>Title: {this.props.title}</div> <div>Body: {this.props.body}</div> <div>Published: {this.props.published}</div> <div>Published By: {this.props.publishedBy}</div> </div> ); } });