Learn how to handle 404s and implement loading states for Logo Link
When working with Logo Link, implementing proper fallback handling ensures a smooth user experience. While we provide built-in fallback options, here’s how to implement custom fallback handling for 404s and loading states in React.
Our React approach combines the basic fallback structure with error handling and loading states:
Copy
Ask AI
const LogoLink = ({ domain, clientId, fallbackText = "N / A" }) => { const [isLoading, setLoading] = useState(true); const [isError, setError] = useState(false); // Transparent 1x1 pixel PNG used as initial placeholder const transparent = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=`; // Construct the logo URL with domain and client ID const logoLink = `https://cdn.brandfetch.io/${domain}/fallback/404/icon?c=${clientId}`; // Determine which source to use based on component state // - During loading: show transparent placeholder // - After loading, no error: show actual logo // - On error: transparent placeholder remains while fallback UI shows const src = !isLoading && !isError ? logoLink : transparent; return ( <div className="wrapper"> <img onLoad={() => setLoading(false)} onError={() => setError(true)} alt="Logo by Brandfetch" className="icon" src={src} /> {/* Fallback UI shown only when there's an error loading the logo */} {isError && ( <div className="fallback"> <p>{fallbackText}</p> </div> )} </div> );};