Skip to content

Commit

Permalink
lazy load react component
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolm-kee committed Feb 9, 2019
1 parent 55cfe8a commit ec5994e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import React from 'react';
import { loadMovies } from './api';
import { BusyContainer } from './components/busy-container';
import { Button } from './components/button';
import { Movie } from './components/movie';
import { TitleBar } from './components/title-bar';
import { useToggle } from './hooks/use-toggle';
import { MovieForm } from './movie-form';

const Movie = React.lazy(() => import('./components/movie'));

function useMovieData() {
const [movies, setMovies] = React.useState([]);
const [isLoading, setIsLoading] = React.useState(true);
Expand Down Expand Up @@ -76,14 +77,16 @@ function App() {
</div>
{moviesShown && (
<BusyContainer isLoading={isLoading}>
{movies.map(movie => (
<Movie
name={movie.name}
releaseDate={movie.releaseDate}
onClick={() => selectMovie(movie)}
key={movie.id}
/>
))}
<React.Suspense fallback={<span className="spinner" />}>
{movies.map(movie => (
<Movie
name={movie.name}
releaseDate={movie.releaseDate}
onClick={() => selectMovie(movie)}
key={movie.id}
/>
))}
</React.Suspense>
</BusyContainer>
)}
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ export const Movie = props => (
<h2>{props.releaseDate}</h2>
</div>
);

export default Movie;

0 comments on commit ec5994e

Please sign in to comment.