← React Beginner Questions - Set 5 Q45
React Beginner Questions - Set 5
Hydration attaches React to server-generated HTML instead of replacing it — provided the markup matches exactly.
// client.js
import { hydrateRoot } from 'react-dom/client';
import App from './App';
hydrateRoot(document.getElementById('root'), <App />);
// Markup that does not match produces hydration errors:
// server: <p>2025-05-12 12:00</p>
// client: <p>2025-05-12 12:01</p> <- different string, React cannot reconcile
// Fix: render time-dependent values after mount
function Clock() {
const [now, setNow] = useState(null);
useEffect(() => setNow(new Date().toISOString()), []);
return <p>{now ?? 'loading'}</p>;
}