@endiliey/react-ideal-image

中文文档
0.0.11发布于 2019/11/24MITTypeScriptCommonJS零依赖近 30 天下载 2.3万

Adaptive image component

原始使用文档
文档目录21 节
  1. The problem
  2. This solution
  3. Table of Contents
  4. Installation
  5. Usage
  6. Props
  7. getIcon
  8. getMessage
  9. getUrl
  10. height
  11. icons
  12. loader
  13. placeholder
  14. shouldAutoDownload
  15. srcSet
  16. theme
  17. threshold
  18. width
  19. Other Solutions
  20. Contributors
  21. LICENSE

react-ideal-image

Adaptive image component


Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Code of Conduct

Watch on GitHub Star on GitHub Tweet

The problem

I need React component to asynchronously load images, which will adapt based on network, which will allow a user to control, which image to load.

This solution

Read the introduction.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install react-ideal-image --save

This package also depends on react, prop-types, and react-waypoint. Please make sure you have those installed as well.

Usage

Example for create-react-app (you need v2 for macros) based project

import React from 'react'
import lqip from 'lqip.macro'
import IdealImage from 'react-ideal-image'

import image from './images/doggo.jpg'
const lqip = lqip('./images/doggo.jpg')

const App = () => (
  <IdealImage
    placeholder={{lqip}}
    srcSet={[{src: image, width: 3500}]}
    alt="doggo"
    width={3500}
    height={2095}
  />
)

Props

This is the list of props that you need to pass to the component.

getIcon

function(state: object) | optional, default icon is provided based on state object

This function decides what icon to show based on the current state of the component.

getMessage

function(icon: string, state: object) | optional, default message is provided based on the icon and state object.

This function decides what message to show based on the icon (returned from getIcon prop) and the current state of the component.

getUrl

function({}) | optional, no useful default

This function is called as soon as the component enters the viewport and is used to generate urls based on width and format if props.srcSet doesn't provide src field.

height

number | required

The Height of the image in px.

icons

object | required

This provides a map of the icons. By default, the component uses icons from material design, implemented as React components with the SVG element. You can customize icons

const icons = {
  load: DownloadIcon,
  //...
}

loader

string | optional, defaults to 'xhr'

This prop takes one of the 2 options, xhr and image. Read more about it here.

placeholder

object | required

This takes one of the 2 objects

// To add a solid color placeholder
{
  color: ''
}

or

/**
 * To add a low quality image
 * [Low Quality Image Placeholder](https://github.com/zouhir/lqip)
 * [SVG-Based Image Placeholder](https://github.com/technopagan/sqip)
 * base64 encoded image of low quality
 */
{
  lqip: ''
}

Read more about it here.

shouldAutoDownload

function({}) | optional, default function is provided which decides based on the device network.

This function decides if image should be downloaded automatically. The default function returns false for a 2g network, for a 3g network it decides based on props.threshold and for a 4g network it returns true by default.

srcSet

array[srcType: object] | required

This provides an array of sources of different format and size of the image. Read more about it here. The srcType has below structure

srcType = {
  width: number, // required
  src: string,
  size: number,
  format: string, // one of the 'jpeg' or 'webp'
}

theme

object | required

This provides a theme to the component. By default, the component uses inline styles, but it is also possible to use CSS modules and override all styles.

const theme = {
  placeholder: {
    backgroundSize: 'cover',
    backgroundRepeat: 'no-repeat',
    position: 'relative',
  },
  // ...
}

threshold

number | optional

Tells how much to wait in milliseconds until consider the download to be slow.

width

number | required

Width of the image in px.

Other Solutions

Contributors

Thanks goes to these people (emoji key):


stereobooster

💻 📖 🚇 ⚠️

Ivan Babak

📖

Arun Kumar

📖

Andrew Lisowski

💻

Timothy Vernon

⚠️

vishalShinde

📖

Evgeniy Kumachev

📖

John Munn

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

Code - MIT

Icons - Apache License 2.0

版本与发布记录

@endiliey/react-ideal-image 共发布 6 个版本,首次发布于 2019/11/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.11latest2019/11/246 年前
0.0.102019/11/196 年前
0.0.92019/11/196 年前
0.0.82019/11/137 年前
0.0.72019/11/137 年前
0.0.62019/11/137 年前

关于 @endiliey/react-ideal-image 的常见问题

@endiliey/react-ideal-image 是什么?

Adaptive image component

如何安装 @endiliey/react-ideal-image?

在项目目录运行 npm install @endiliey/react-ideal-image,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:prop-types(>=15)、react(>=0.14.x)、react-waypoint(>=9.0.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@endiliey/react-ideal-image 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@endiliey/react-ideal-image 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@endiliey/react-ideal-image 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 8.9.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@endiliey/react-ideal-image 可以商用吗?

@endiliey/react-ideal-image 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@endiliey/react-ideal-image 的下载量有多少?

根据 npm 官方下载统计,@endiliey/react-ideal-image 在 2026/10/14 – 2026/11/12 这 30 天内被下载 22,837 次,日均约 761 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@endiliey/react-ideal-image 还在维护吗?

最新版本 0.0.11 发布于 2019/11/24(6 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/11/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。