Dev Helpers
← Back

Photo load fallback

Import and render <PhotoLoadFallback /> in a React project. Provide public/studio.jpg or pass your own image source.

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%" }}
    />
  );
}