coforma/swift-tech-challenge

View on GitHub
src/app/components/utilities/ImageWithFallback.tsx

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { useState } from "react";
import fallbackImage from "../../assets/default-institution-image.png";
import Image from "next/image";

export const ImageWithFallback = ({
  src,
  alt,
  height,
  width,
  ...props
}: Props) => {
  const [error, setError] = useState(false);

  return (
    <Image
      height={height}
      width={width}
      onError={() => setError(true)}
      src={error ? fallbackImage : src}
      alt={error ? "Placeholder image" : alt}
      {...props}
    />
  );
};

type Props = {
  src: string;
  alt: string;
  width: number;
  height: number;
};