![]() |
In web applications, handling images is crucial for a better user experience. Sometimes, images can fail to load due to various reasons like wrong URLs, server errors, network issues or browser caching issues. The better approach for handling an image error enhances the robustness of the application. In this article, we’ll explore how to check and manage broken images. PrerequisitesThese are the following approaches: Table of Content What are broken images?The broken images come up whenever an HTML ‘<img>’ element is attempting to load an unavailable or invalid source. This shows a broken image icon, which offers a bad user experience and the application fails to deliver good quality. Folder Structure:![]() Folder Strcutre Dependencies:"dependencies": { Steps to handle broken imagesStep 1: Install Node.jsBefore creating an application ensure the Node.js is installed on your system Step 2: Create a new React application
npm create vite@latest react-broken-image -- --template react Step 3: Redirect to project directoryChange you working directory by entering project name in the below command cd react-broken-image Step 4: npm installRun the below command to install required dependencies npm install Using On-Error Event HandlerCreate an ‘ImageWithDefault’ component for the ‘app’ component. Use the ‘onError’ event handler on the ‘<img>’ element to switch to a default image when loading fails. Steps include setting up state and the ‘useEffect’ hook for managing image URLs and error handling. Pass ‘src’, ‘defaultSrc’, and ‘altInfo’ as props from the parent component to ‘ImageWithDefault’, which features a ‘handleError’ method for image source updates on load failure. Example: To demonstrate checking and resolving broken image in RectJS uisng the on-error event handler.
We pass ‘src’ and ‘defaultSrc’ from the parent ‘app’ component to the child ‘ImageWithDefault’ component. The ‘src’ prop is stored in the ‘imageSrc’ state. An ‘onError’ function triggers on error, using the ‘setImageSrc’ setter to switch to ‘defaultSrc’ if the primary ‘src’ fails. Using Higher-Order Component (HOC)Create a ‘ImageWithError’ higher-order component (HOC) for use in the ‘app’ component. This HOC adds error handling capabilities to components that output ‘<img>’ elements, making it ideal for large-scale applications with numerous image-rendering components. Steps involve creating the HOC to wrap image-rendering components, providing straightforward ‘onError’ handling to manage broken images. Example: To demonstrate checking and resolving broken image in RectJS uisng the Higher-Order Component.
Execute an applicationRun the below command to execute an application after implementation of the codes. npm run dev Output: ![]() How To Check And Resolve Broken Images In React JS Note: The output will be same for both of the approaches. |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |