{"id":13450544,"url":"https://github.com/robcalcroft/react-use-lazy-load-image","last_synced_at":"2025-04-10T19:51:00.848Z","repository":{"id":35049225,"uuid":"200247058","full_name":"robcalcroft/react-use-lazy-load-image","owner":"robcalcroft","description":":sunrise: :zap: Add image lazy loading to your React app with ease","archived":false,"fork":false,"pushed_at":"2023-01-04T06:11:01.000Z","size":1147,"stargazers_count":14,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T23:04:10.201Z","etag":null,"topics":["hooks","image","lazyload","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robcalcroft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-02T14:17:39.000Z","updated_at":"2023-07-11T03:41:09.000Z","dependencies_parsed_at":"2023-01-15T12:45:18.210Z","dependency_job_id":null,"html_url":"https://github.com/robcalcroft/react-use-lazy-load-image","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robcalcroft%2Freact-use-lazy-load-image","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robcalcroft%2Freact-use-lazy-load-image/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robcalcroft%2Freact-use-lazy-load-image/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robcalcroft%2Freact-use-lazy-load-image/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robcalcroft","download_url":"https://codeload.github.com/robcalcroft/react-use-lazy-load-image/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248283275,"owners_count":21077811,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","image","lazyload","react"],"created_at":"2024-07-31T07:00:35.870Z","updated_at":"2025-04-10T19:51:00.821Z","avatar_url":"https://github.com/robcalcroft.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# react-use-lazy-load-image :sunrise: :zap:\n\n\u003e Add image lazy loading to your React app with ease\n\n`react-use-lazy-load-image` uses the [`IntersectionObserver`](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) to provide a performant solution to lazy loading images that doesn't involve scroll event listeners. The `IntersectionObserver` API is still quite new so older browsers may not support this, however there are some good polyfills available for these use cases.\n\n`react-use-lazy-load-image` is super light weight so won't add any extra bulk to your app.\n\nAs the name suggests `react-use-lazy-load-image` uses React Hooks, so you need to be using React function components to use this library.\n\n### Usage\n\n1. Add a `data-img-src` attribute ([you can customise this](#arguments)) to your `img` tags as your main image source\n2. Change the `src` attribute to a placeholder like a small data URL blob\n3. Import and run `useLazyLoadImage` in the body of your React function component\n\nNow as your users scroll down the page the images will load just in time.\n\n### Arguments\n\n| Argument name     | Default value     | Description                                                                                              |\n|-------------------|-------------------|----------------------------------------------------------------------------------------------------------|\n| imageAttribute    | `'[data-img-src]'`| The query passed to `document.querySelectorAll` to grab all lazy load-able images on the page            |\n| imageAttributeKey | `'imgSrc'`        | The camel-cased key to pull the `data-img-src` out of the image element                                  |\n| rootMargin        | `'200px 0px'`     | https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options |\n| threshold         | `0.01 `           | https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Intersection_observer_options |\n| debug             | `false`           | When set to `true` some useful messages will get logged to the console                                   |\n| dependencies      | `[]`              | React useEffect dependency array, used for re-running this logic if the component re-renders and the `img` references change ([read more about conditionally firing an effect](https://reactjs.org/docs/hooks-reference.html#conditionally-firing-an-effect)) |\n\n\n### Example\n\n```javascript\nimport React from 'react';\nimport useLazyLoadImage from 'react-use-lazy-load-image';\n\nfunction App() {\n  useLazyLoadImageReact();\n\n  return (\n    \u003cdiv\u003eLots of content that means the image is off screen goes here\u003c/div\u003e\n    \u003cimg src=\"DATA URL\" data-img-src=\"https://link-to-my-image.com/image.png\" alt=\"My image\" /\u003e\n  )\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobcalcroft%2Freact-use-lazy-load-image","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobcalcroft%2Freact-use-lazy-load-image","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobcalcroft%2Freact-use-lazy-load-image/lists"}