H5 实现图片可预览放大等操作对库 react-photo-view 的封装和使用

导读:本篇文章讲解 H5 实现图片可预览放大等操作对库 react-photo-view 的封装和使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

例如,Taro 实现的 H5,Taro对外的提供的预览图片没有放大功能,需要自己重新封装

封装 PreviewImage 组件

import { View } from '@tarojs/components';
import { useMemo } from 'react';
import { PhotoProvider, PhotoSlider } from 'react-photo-view';
import 'react-photo-view/dist/react-photo-view.css';

import './index.less';

interface IProps {
  visible: boolean;
  current: number;
  urls: string[];
  onClose: () => void;
  onChange?: (arg: number) => void;
  photoClosable?: boolean;
}

const PreviewImage: React.FC<IProps> = ({
  visible,
  current,
  urls = [],
  onClose,
  onChange,
  photoClosable = false,
  ...rest
}) => {
  const urlsArr = useMemo(() => {
    if (urls && !!urls.length) {
      const _urls = urls.filter(v => v);
      return _urls.map((item, index) => ({ src: item, key: index }));
    }
    return [];
  }, [urls]);

  return (
    <View className='xp-preview-image'>
      <PhotoProvider {...rest}>
        <PhotoSlider
          images={urlsArr}
          photoClosable={photoClosable}
          visible={visible}
          onClose={onClose}
          index={current}
          onIndexChange={onChange}
        />
      </PhotoProvider>
    </View>
  );
};
export default PreviewImage;

使用

  const [visible, setVisible] = useState<boolean>(false);
  const [current, setCurrent] = useState<number>(0);
  {visible && (
    <PreviewImage
      visible={visible}
      current={current}
      urls={[]}
      onClose={() => {
        setVisible(false);
      }}
      onChange={e => {
        setCurrent(e);
      }}
    />
  )}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/64451.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!