Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ This doesn't mean you shouldn't use non-idempotent functions like `new Date()` _
import { useState, useEffect } from 'react';

function useTime() {
// 1. Keep track of the current date's state. `useState` receives an initializer function as its
// initial state. It only runs once when the hook is called, so only the current date at the
// time the hook is called is set first.
const [time, setTime] = useState(() => new Date());
// 1. Start without a time until the first tick.
const [time, setTime] = useState(null);

useEffect(() => {
// 2. Update the current date every second using `setInterval`.
Expand All @@ -110,7 +108,7 @@ function useTime() {

export default function Clock() {
const time = useTime();
return <span>{time.toLocaleString()}</span>;
return <span>{time === null ? 'Loading…' : time.toLocaleString()}</span>;
}
```

Expand Down
Loading