Skip to content

Instantly share code, notes, and snippets.

@phpnode
Created March 9, 2017 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpnode/41b85d2917f8bd09dae0edfa79186d88 to your computer and use it in GitHub Desktop.
Save phpnode/41b85d2917f8bd09dae0edfa79186d88 to your computer and use it in GitHub Desktop.
// This is how the world looks at the moment
import {render} from 'react-dom';
render(
<Router>
<Route path="/" component={HomeScreen} />
</Router>,
document.getElementById('root')
);
// So the router exists within the render call, it doesn't control render, it itself is being rendered.
// This means it can't control *when* render happens.
// What if we switched this around?
function run (router, element) {
const route = getActiveRoute(router);
const promise = extractPromise(route);
if (promise) {
promise.then(data => render(React.cloneElement(route, {data}), element));
}
else {
render(route, element);
}
}
const router = (
<Router>
<Route path="/" component={HomeScreen} async={() => Promise.resolve(true)}/>
</Router>
);
run(router, document.getElementById('root'));
// In this model the router is in charge of calling render() when it
// has loaded the data it needs. There's no flash of loading screen
// etc because render doesn't happen until the promise has been resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment