例如,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