react-native#ImageProps TypeScript Examples

The following examples show how to use react-native#ImageProps. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CachedImage.tsx    From companion-kit with MIT License 5 votes vote down vote up
export function CachedImage(this: void, props: CachedImageProps) {

    const [imgURI, setImgURI] = React.useState('');
    const state = React.useMemo(() => ({ loading: false, mounted: false }), []);

    const safeCall = React.useCallback((cb: () => any) => {
        if (state.mounted) {
            cb();
        }
    }, [state]);

    const loadImage = async (filesystemURI: string, remoteURI: string) => {
        try {
            // Use the cached image if it exists
            const metadata = await FileSystem.getInfoAsync(filesystemURI);
            if (metadata.exists) {
                safeCall(() => setImgURI(filesystemURI));
                return;
            }

            // otherwise download to cache
            const imageObject = await FileSystem.downloadAsync(
                remoteURI,
                filesystemURI,
            );
            safeCall(() => setImgURI(imageObject.uri));
        } catch (err) {
            console.log('Image loading error:', err);
            safeCall(() => setImgURI(remoteURI));
        }
    };

    React.useEffect(() => {
        state.mounted = true;

        (async () => {
            const uri = (props.source as ImageURISource).uri;
            if (uri) {
                const filesystemURI = await getImageFilesystemKey(uri);
                if (imgURI !== uri && imgURI !== filesystemURI) {
                    await loadImage(filesystemURI, uri);
                }
            }
        })();

        return () => {
            state.mounted = false;
        };

    }, [(props.source as ImageURISource).uri]);

    let source: ImageProps['source'] = imgURI ? { uri: imgURI } : null;
    if (!source && props.source) {
        source = props.source;
    }

    if (props.isBackground) {
        return (
            <ImageBackground
                {...props}
                source={source}
            >
                {props.children}
            </ImageBackground>
        );
    } else {
        return (
            <Image
                {...props}
                source={source}
            />
        );
    }
}
Example #2
Source File: Image.tsx    From react-native-jigsaw with MIT License 5 votes vote down vote up
Image: React.FC<ImageProps> = ({
  source,
  resizeMode = "cover",
  style,
  ...props
}) => {
  let imageSource =
    source === null || source === undefined
      ? Config.placeholderImageURL
      : source;

  const styles = StyleSheet.flatten(style || {});
  const { aspectRatio, width, height } = generateDimensions(
    styles as ImageStyleProp
  );

  if (aspectRatio) {
    return (
      <AspectRatio style={[style, { width, height, aspectRatio }]}>
        <NativeImage
          {...props}
          source={imageSource as ImageSourcePropType}
          resizeMode={resizeMode}
          style={[
            style,
            {
              height: "100%",
              width: "100%",
            },
          ]}
        />
      </AspectRatio>
    );
  }

  return (
    <NativeImage
      {...props}
      source={source as ImageSourcePropType}
      resizeMode={resizeMode}
      style={style}
    />
  );
}
Example #3
Source File: AuthorCard.tsx    From nyxo-app with GNU General Public License v3.0 5 votes vote down vote up
Avatar = styled(FastImage)<ImageProps>`
  width: 30px;
  height: 30px;
  border-radius: 15px;
  overflow: hidden;
  margin-right: 20px;
`