Skip to content

Commit 3d4c59c

Browse files
use placeholder to prevent flash of loading message
1 parent 1c06609 commit 3d4c59c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import Coroutine from 'react-coroutine';
3+
4+
export default Coroutine.create(Placeholder);
5+
6+
async function Placeholder({ delay, children }) {
7+
await new Promise(resolve => setTimeout(resolve, delay));
8+
return children;
9+
}

examples/code-splitting/modules/Routes.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import Coroutine from 'react-coroutine';
33
import { BrowserRouter as Router, Route } from 'react-router-dom';
44
import Pokemons from './PokemonAPI';
5+
import Placeholder from './Placeholder';
56

67
/* Routes are using wrapped by Coroutine components for the sake of
78
async functions and generators usage. */
@@ -35,9 +36,13 @@ async function* PokemonInfoLoader({ match }) {
3536
/* This request can also be cached but that's API's implementation detail.
3637
For the example purpose, it just does new request all the time. */
3738
const pokemonInfo = Pokemons.retrieve(match.params.pokemonId);
38-
/* Since API request takes time, we show a pending message immediately
39+
/* Since API request takes time sometimes, we show a pending message
3940
and then wait for requests resolving. */
40-
yield <p>Loading...</p>;
41+
yield (
42+
<Placeholder delay={2000}>
43+
<p>Loading...</p>
44+
</Placeholder>
45+
);
4146
/* Promise.all is used pretty much for example purpose. However, it's
4247
efficient way to make concurrent requests. */
4348
const [{ default: PokemonInfo }, data] = await Promise.all([module, pokemonInfo]);

0 commit comments

Comments
 (0)