From 490dfafa2f09f27327c02856c71e44605d26e83e Mon Sep 17 00:00:00 2001 From: Abhijeet Kumar Date: Mon, 30 Nov 2026 00:13:29 +0530 Subject: [PATCH] Read the clock outside render in the purity example --- .../reference/rules/components-and-hooks-must-be-pure.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/content/reference/rules/components-and-hooks-must-be-pure.md b/src/content/reference/rules/components-and-hooks-must-be-pure.md index 973561c2251..6749fb6fdd5 100644 --- a/src/content/reference/rules/components-and-hooks-must-be-pure.md +++ b/src/content/reference/rules/components-and-hooks-must-be-pure.md @@ -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`. @@ -110,7 +108,7 @@ function useTime() { export default function Clock() { const time = useTime(); - return {time.toLocaleString()}; + return {time === null ? 'Loading…' : time.toLocaleString()}; } ```