import {FC, useEffect, useState} from "react"; import {Image, ImageProps, View} from "@tarojs/components"; import {AtActivityIndicator} from "taro-ui"; import shard from '@/static/img/shard.png' interface Props extends ImageProps { width: number height: number fallback?: string } const Img: FC = ({src, mode = 'aspectFill', width, height, fallback = shard}) => { const [isError, setIsError] = useState(false) const [loading, setLoading] = useState(true) useEffect(() => { if (!src) { setIsError(true) setLoading(false) } else { setIsError(false) setLoading(false) } }, [src]) // 图片加载失败 function onErrorHandler() { setLoading(false) setIsError(true) } function onLoadHandler() { setLoading(false) setIsError(false) } return ( {!isError && } { loading && } { isError && !loading && } ) } export default Img