Part 14: ES6 Refactor Routing without Mixins
Home.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import React from 'react'; class Home extends React.Component { render () { return ( <h2 className="text-center"> Search by Github Username Above </h2> ) } }; export default Home; /*var React = require('react'); var Home = React.createClass({ render: function () { return ( <h2 className="text-center"> Search by Github Username Above </h2> ) } }); module.exports = Home;*/ |
Main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import React from 'react'; import { RouteHandler } from 'react-router'; import SearchGithub from './SearchGithub'; class Main extends React.Component { render () { return ( <div className="main-container"> <nav className="navbar navbar-default" role="navigation"> <div className="col-sm-7 col-sm-offset-2" style={{marginTop: 15}}> <SearchGithub /> </div> </nav> <div className="container"> <RouteHandler {...this.props} /> </div> </div> ) } } export default Main; /*var React = require('react'); var RouteHandler = require('react-router').RouteHandler; var SearchGithub = require('./SearchGithub'); var Main = React.createClass({ render: function () { return ( <div className="main-container"> <nav className="navbar navbar-default" role="navigation"> <div className="col-sm-7 col-sm-offset-2" style={{marginTop: 15}}> <SearchGithub /> </div> </nav> <div className="container"> <RouteHandler /> </div> </div> ) } }); // We don't want this file to decide when to render this component, // so we are removing it //React.render(<Main />, document.getElementById('app')); // Instead we are going to expert the compontent, so that whenever you // do require('Main') it will render the compontent above. module.exports = Main;*/ |
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import React from 'react'; import Router from 'react-router'; import routes from './config/routes'; // This file will be in charge of the render methods /*Router.run(routes, function (Root) { // Similar to what we did initially in Main.js, but instead the component is replaced dynamically and rendered here; React.render(<Root />, document.getElementById('app')); });*/ Router.run(routes, (Root, state) => { React.render (<Root {...state} />, document.getElementById('app')); }); |
SearchGithub.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import React from 'react'; class SearchGithub extends React.Component { handleSubmit () { // We pass the router through this.context so that we can use it with the // .transitionTo method as this. is not referring to the mixing function in our ES6 refactor var router = this.context.router; var username = this.refs.username.getDOMNode().value; this.refs.username.getDOMNode().value=''; router.transitionTo('profile', {username: username}); // Available from the Router.Navigation mixing as a property } render () { return ( <div className="col-sm-12"> <form onSubmit={this.handleSubmit.bind(this)}> <div className="form-group col-sm-7"> <input type="text" className="form-control" ref="username" /> </div> <div className="form-group col-sm-5"> <button type="submit" className="btn btn-block btn-primary">Search Github</button> </div> </form> </div> ) } }; // Because in ES6 we can't use mixins, we need to pass the function to the component // through the property contextTypes which then can access the function within the component SearchGithub.contextTypes = { router: React.PropTypes.func.isRequired }; export default SearchGithub; /*var React = require('react'); var Router = require('react-router'); var SearchGithub = React.createClass({ mixins: [Router.Navigation], handleSubmit: function () { var username = this.refs.username.getDOMNode().value; this.refs.username.getDOMNode().value=''; this.transitionTo('profile', {username: username}); // Available from the Router.Navigation mixing as a property // We pass the route we want to transition to - in routes.js we defined the router as "profile" = <Route name="profile" path="profile/:username" handler={Profile} /> }, render: function () { return ( <div className="col-sm-12"> <form onSubmit={this.handleSubmit}> <div className="form-group col-sm-7"> <input type="text" className="form-control" ref="username" /> </div> <div className="form-group col-sm-5"> <button type="submit" className="btn btn-block btn-primary">Search Github</button> </div> </form> </div> ) } }); module.exports = SearchGithub;*/ |