Skip to content

Instantly share code, notes, and snippets.

@taion
Created August 5, 2016 16:55
Show Gist options
  • Select an option

  • Save taion/935f6417551380fa95d0f7f67964b83a to your computer and use it in GitHub Desktop.

Select an option

Save taion/935f6417551380fa95d0f7f67964b83a to your computer and use it in GitHub Desktop.

Revisions

  1. taion created this gist Aug 5, 2016.
    54 changes: 54 additions & 0 deletions Route.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    import invariant from 'invariant';
    import React from 'react';
    import BaseRoute from 'react-router/lib/Route';

    import LoadingIndicator from './LoadingIndicator';

    // This is not a component.
    /* eslint-disable react/prop-types */
    function render({ props, element }) {
    if (!props) {
    return <LoadingIndicator />;
    }

    if (this.renderFetched) {
    return this.renderFetched(props);
    }

    return React.cloneElement(element, props);
    }
    /* eslint-enable react/prop-types */

    function createRoute(element) {
    const route = BaseRoute.createRouteFromReactElement(element);

    if (!route.render && (route.queries || route.getQueries)) {
    route.render = render;
    }

    return route;
    }

    function Route() {
    invariant(false, '`<Route>` should not be rendered.');
    }

    Route.createRouteFromReactElement = createRoute;

    export default Route;

    function createIndexRoute(element, parentRoute) {
    invariant(parentRoute, 'An `<IndexRoute>` must have a parent.');

    /* eslint-disable no-param-reassign */
    parentRoute.indexRoute = createRoute(element);
    /* eslint-enable no-param-reassign */
    }

    function IndexRoute() {
    invariant(false, '`<IndexRoute>` should not be rendered.');
    }

    IndexRoute.createRouteFromReactElement = createIndexRoute;

    export { IndexRoute };