Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active May 8, 2019 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanflorence/6b00227745a9593ea93a151e34abdda7 to your computer and use it in GitHub Desktop.
Save ryanflorence/6b00227745a9593ea93a151e34abdda7 to your computer and use it in GitHub Desktop.
import "app/index.css"
import "app/App.css"
import "./styles.css"
import React, { useState, useEffect } from "react"
import ReactDOM from "react-dom"
function App() {
const [appTime] = useState(Date.now())
return (
<div>
<h1>App</h1>
<TruckPopup time={appTime} />
</div>
)
}
function shouldWarn(now, time) {
return now - time > 2000
}
function TruckPopup({ time }) {
const [now, setNow] = useState(Date.now())
const [warning, setWarning] = useState()
if (!warning && shouldWarn(now, time)) {
setWarning(true)
}
useEffect(() => {
if (!warning) {
if (shouldWarn(now, time)) {
setWarning(true)
} else {
setTimeout(() => setNow(Date.now()), 100)
}
}
}, [now, time, warning])
return (
<div>
<h2>Truck Popup</h2>
{warning && <p style={{ color: "red" }}>WARNING</p>}
</div>
)
}
ReactDOM.render(<App />, document.getElementById("root"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment