Open this example in the interactive library
Live preview
Source code
// components/PhotoLoadFallback.tsx
import { useState } from "react";
export default function PhotoLoadFallback({
src = "/studio.jpg",
caption = "Inside the studio",
}: { src?: string; caption?: string } = {}) {
const [failed, set] = useState<string | null>(null);
return failed === src ? (
<p>{caption} — image unavailable</p>
) : (
<img
src={src}
alt={caption}
onError={() => set(src)}
style={{ maxWidth: "100%" }}
/>
);
}
