Meet Pete Hunt of the Core React Team.
Pete says amazing things like:
"Reduce coupling, increase cohesion"
"Mutable state is the root of all evil."
"React components are just idempotent functions"
..and he can back up those statements.
Technical Architect at Universal Mind
Instructor at egghead.io
youtube.com/joemaddalone
JavascriptOO.com
Filltext.com
NOT AS COOL AS PETE HUNT
"Reduce coupling, increase cohesion"
Use JavaScript, not templates
"Mutable state is the root of all evil."
State is hard. Just Re-render everything.
"React components are just idempotent functions"
React components are just functions
return BaseView.extend( { initialize: function ( options ) { new Collection().fetch().then(function(data){ this.collection = data; }) }, render: function () { var template = _.template( myViewTemplate, {collection: this.collection} ); this.$el.html( template ); } } );
<ul> <% _.each( collection, function( item ){ %> <li><%= item.get("title") %></li> <% }); %> </ul>
return BaseView.extend( { events: { 'click .filterModel': 'aHandler' }, aHandler: function( e ){ e.preventDefault(); renderAgain( _.extend( this.collection, { model: $( e.currentTarget ).data( 'modelNumber' ) } ) ); }, render: function () {...} renderAgain: function ( newState ) {...} } );
<ul> <% _.each( collection, function( item ){ %> <li> <p><%= item.get("title") %></p> <button class="filterModel" data-modelNumber="<%= item.get( "modelNumber" ) %>"> This Models </button> </li> <% }); %> </ul>
{{#each item in collection}} <!-- loop --> {{#if item.active}} <!-- conditional --> {{item.title}} {{/if}} {{/each}}
<div ng-repeat="item in collection"> <!-- loop --> <div ng-show="item.active"> <!-- conditional --> {{item.title}} </div> </div>
var HelloWorld = document.createElement('h1'); HelloWorld.id = "Hello"; HelloWorld.innerHTML = "Hello World"; document.body.appendChild(HelloWorld);
React.render(React.DOM.h1({id: 'Hello'}, 'Hello World'), document.body);
<body> <h1 id="Hello">Hello World</h1> </body>
var HelloWorld = React.createClass({ render: function(){ return React.DOM.h1({id: 'Hello'}, 'Hello World'); } }) React.render(HelloWorld(), document.body);
<body> <h1 id="Hello">Hello World</h1> </body>
var HelloWorld = React.createClass({ render: function(){ return <h1 id="Hello">Hello World</h1> } }) React.render(<HelloWorld />, document.body);
<body> <h1 id="Hello">Hello World</h1> </body>
"Reduce coupling, increase cohesion"
Use JavaScript, not templates
render: function(){ return <h1>Hello World!</h1> }
of awesomesauce
Here's the kool-aid you need to drink.
$(document).on('click', '.show', function(){ React.render(Component(), someElement); }); $(document).on('click', '.remove', function(){ React.unmountComponentAtNode(someElement); })
var App = React.createClass({ render:function(){ return ( <select> <option value="AL">Alabama</option> ... <option value="WY">Wyoming</option> </select> ) }, componentDidMount:function(){ $(this.getDOMNode()).selectPlugin(); }, });
var test = React.addons.TestUtils; describe('Component', function() { it('should have a val of 1', function() { var c = test.renderIntoDocument(Component()); test.Simulate.click(c.refs.buttonA.getDOMNode()); c.state.val.should.equal(1); }); });
http.createServer(function(req, res) { var props = {items: [0, 1]} var myAppHtml = React.renderToString(MyApp(props)) /* OR */ var myAppHtml = React.renderToStaticMarkup(MyApp(props)) res.setHeader('Content-Type', 'text/html') res.end(myAppHtml) }).listen(3000)