Created
January 25, 2023 21:59
-
-
Save cindywu/bce0af0114fb0a48445c4f8b657f7191 to your computer and use it in GitHub Desktop.
time-in-color.tsx
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, { useState } from 'react' | |
export default function Home() { | |
const colors : string[] = ["bg-emerald-500", "bg-red-500", "bg-yellow-500", "bg-emerald-800", "bg-emerald-900"] | |
return ( | |
<div className={"h-screen"}> | |
<ColorBox | |
colors={colors} | |
/> | |
</div> | |
) | |
} | |
type ColorBoxProps = { | |
colors: string[] | |
} | |
function ColorBox({colors} : ColorBoxProps){ | |
console.log({colors}) | |
const [color, setColor] = useState<string>(colors[0]) | |
// map through the colors array and return a div for each color | |
const colorBoxes = colors.map((color) => { | |
return ( | |
<div className={`h-screen + ${color}`}> | |
</div> | |
) | |
}) | |
const start = Date.now(); | |
console.log('starting timer...'); | |
setTimeout(() => { | |
const millis = Date.now() - start; | |
console.log(`seconds elapsed = ${Math.floor(millis / 1000)}`); | |
setColor(colors[1]) | |
// Expected output: "seconds elapsed = 2" | |
}, 2000); | |
return ( | |
<div className={`h-screen + ${color}`}> | |
color box | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment