Skip to content

Instantly share code, notes, and snippets.

@gregberge
Created November 7, 2018 11:09
Show Gist options
  • Save gregberge/f031a166e0f4bee1ebbc5268c4182a83 to your computer and use it in GitHub Desktop.
Save gregberge/f031a166e0f4bee1ebbc5268c4182a83 to your computer and use it in GitHub Desktop.
useCountdown
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
}
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