Skip to content

Instantly share code, notes, and snippets.

@faahmad
Created March 6, 2020 16:25
Show Gist options
  • Select an option

  • Save faahmad/bd2590db6fdeadf11d6e4287fd7c2f0a to your computer and use it in GitHub Desktop.

Select an option

Save faahmad/bd2590db6fdeadf11d6e4287fd7c2f0a to your computer and use it in GitHub Desktop.
Opinionated React 3: State Management
const MovieList: React.FC = () => {
const [isLoading, setIsLoading] = React.useState<boolean>(true)
const [movies, setMovies] = React.useState<Movie[]>([])
React.useEffect(() => {
MovieService
.fetchInitialMovies()
.then(initialMovies => setMovies(initialMovies))
.then(() => setIsLoading(false))
}, [])
if (isLoading) {
return <div>Loading movies...</div>
}
return (
<ul>
{movies.map(movie => <li key={movie.id}>{movie.title}</li>}
</ul>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment