Skip to content

Instantly share code, notes, and snippets.

@gnz00
Created October 4, 2015 12:18
Show Gist options
  • Save gnz00/7ad5b353827b5cbafb4e to your computer and use it in GitHub Desktop.
Save gnz00/7ad5b353827b5cbafb4e to your computer and use it in GitHub Desktop.
React-Nprogress
/*! React Starter Kit | MIT License | http://www.reactstarterkit.com/ */
import React from 'react';
import Router from 'react-routing/src/Router';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import http from './core/HttpClient';
import App from './components/App';
import IndexPage from './components/IndexPage';
import NotFoundPage from './components/NotFoundPage';
import ErrorPage from './components/ErrorPage';
import NProgress from 'react-nprogress';
const router = new Router(on => {
on('*', async (state, next) => {
if (canUseDOM)
NProgress.start();
const component = await next();
if (canUseDOM)
NProgress.done();
return component && <App context={state.context}>{component}</App>;
});
on('/', async () => {
let data = await getDataMock("/api/people");
return <IndexPage name={data}/>
});
on('error', (state, error) => {
console.log(error);
console.log(state);
return state.statusCode === 404 ?
<App context={state.context} error={error}><NotFoundPage /></App> :
<App context={state.context} error={error}><ErrorPage /></App>
}
);
});
export default router;
var getDataMock = function(url) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve([{ name: "Jake" }]);
}, 10000);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment