图像无法渲染 - 挣扎 'react-native-snap-carousel'

Images wont render - Struggling with 'react-native-snap-carousel'

不幸的是,我很难真正掌握这个库的工作原理,特别是 ParallaxImage 组件。我想知道是否有人可以帮助我。我正在努力渲染我的图像。

文档如下:https://www.npmjs.com/package/react-native-snap-carousel#props-methods-and-getters

这里是下面的代码不过我也已经吃零食了运行它here

它不会在网络上加载顺便说一下,您必须选择 IOS 或 android。

import React, {useRef, useState, useEffect} from 'react';
import Carousel, {ParallaxImage} from 'react-native-snap-carousel';
import {
  View,
  Text,
  Dimensions,
  StyleSheet,
  TouchableOpacity,
  Platform,
} from 'react-native';

const ENTRIES1 = [
  {
    title: 'Text',
    thumbnail: require('./assets/splash.png'),
  },
  {
    title: 'Text 1',
    thumbnail: require('./assets/splash.png'),
  },
  {
    title: 'Text 2',
    thumbnail: require('./assets/splash.png'),
  },
];
const {width: screenWidth} = Dimensions.get('window');

const MyCarousel = props => {
  const [entries, setEntries] = useState([]);
  const carouselRef = useRef(null);

  const goForward = () => {
    carouselRef.current.snapToNext();
  };

  useEffect(() => {
    setEntries(ENTRIES1);
  }, []);

  const renderItem = ({item, index}, parallaxProps) => {
    return (
      <View style={styles.item}>
        <ParallaxImage
          source={{uri: item.thumbnail}}
          containerStyle={styles.imageContainer}
          style={styles.image}
          parallaxFactor={0.4}
          {...parallaxProps}
        />
        <Text style={styles.title} numberOfLines={2}>
          {item.title}
        </Text>
      </View>
    );
  };

  return (
    <View style={styles.container}>
      <Carousel
        ref={carouselRef}
        sliderWidth={screenWidth}
        sliderHeight={screenWidth}
        itemWidth={screenWidth - 60}
        data={entries}
        renderItem={renderItem}
        hasParallaxImages={true}
      />
    </View>
  );
};

export default MyCarousel;

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  item: {
    width: screenWidth - 60,
    height: screenWidth - 60,
  },
  imageContainer: {
    flex: 1,
    marginBottom: Platform.select({ios: 0, android: 1}), // Prevent a random Android rendering issue
    backgroundColor: 'white',
    borderRadius: 8,
  },
  image: {
    ...StyleSheet.absoluteFillObject,
    resizeMode: 'cover',
  },
});

我认为您需要将 source={{uri: item.thumbnail}} 替换为 source={item.thumbnail} 因为 uri 用于网络图像 url 就像 https://image.pngrequire 用于本地图像。我在 android 上进行了测试,现在工作正常