Created
November 7, 2018 11:09
-
-
Save gregberge/f031a166e0f4bee1ebbc5268c4182a83 to your computer and use it in GitHub Desktop.
useCountdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
export const useCountdown = ({ initialValue, delay }) => { | |
const [value, setValue] = useState(initialValue) | |
useEffect(() => { | |
if (value > 0) setTimeout(() => setValue(value - 1), delay) | |
}) | |
return value | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { useCountdown } from './Countdown' | |
const Home = ({ progress, error, result }) => { | |
const value = useCountdown({ initialValue: 10, delay: DURATION }) | |
if (value > 0) return <Count key={value}>{value}</Count> | |
if (progress) return 'Loading...' | |
if (error) return 'Oups' | |
return ( | |
<MovieList> | |
{result.data.results.map(movie => ( | |
<Link key={movie.id} to={`/movies/${movie.id}`}> | |
<MovieCard movie={movie} /> | |
</Link> | |
))} | |
</MovieList> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment