【问题标题】:Show loader until live link image does not load completely React native 0.58显示加载器,直到实时链接图像未完全加载 React native 0.58
【发布时间】:2019-08-16 10:15:40
【问题描述】:

我从图像具有实时链接的 api 接收 JSON 数组。所以,在我渲染数据成功后,问题是我想显示一个 gif,直到图像完全加载。

数组是:-

[{
    "category": "Loose Flower",
    "id": "7",
    "product_name": "Drb,Tls,Blpt,Nlknt",
    "unit_price": "0",
    "img_path": "http://prabhujionline.com/bo/upload/product_img/1513948635octa_pooja_basket_w_handle6_580x@2x.jpg",
    "count": 0,
    "is_loaded": true
  },
  {
    "category": "Loose Flower",
    "id": "1",
    "product_name": "Genda",
    "unit_price": "0.5",
    "img_path": "http://prabhujionline.com/bo/upload/product_img/1513947159genda.png",
    "count": 0,
    "is_loaded": true
  }]

<Image
 style={{ height: 212 / 3, width: 300 / 3, borderRadius: 5 }}
 source={(this.state.singlepack.is_loaded) ? { uri: 
 this.state.singlepack.img_path } : 
 require('../../../../assets/flower-loader.gif')}
  onLoadStart={(e) => { 
     this.changeImageLoadStatusOnStart(this.props.singleData.id) }}
  onLoadEnd={(e) => 
  this.changeImageLoadStatusOnEnd(this.props.singleData.id) }
 />


changeImageLoadStatusOnStart = (id)=>{
        this.setState({
            singlepack: {
                ...this.state.singlepack,
                is_loaded : false
            }
        })
    }

    changeImageLoadStatusOnEnd = (id) => {
        this.setState({
            singlepack: {
                ...this.state.singlepack,
                is_loaded: true
            }
        })
    }

我已经尝试过 onLoadStart 和 onLoadEnd 但我无法做出逻辑,我可以帮忙吗?

【问题讨论】:

  • 我仍然希望有一个解决方案。由于 defaultSorce 和 loadingIndicatorSource 无法正常工作。

标签: react-native reactive-programming


【解决方案1】:

defaultSourceloadingIndicatorSource 无法正常工作。我找到了解决办法

const loaderImage = require('./../assets/img/spinner.png');
var {width} = Dimensions.get('window');

this.state = {
  imageloading: true,
};

_renderItemFood(item) {
    return (
      <TouchableOpacity
        style={styles.divFood}>
        <Image
          style={styles.imageFood}
          resizeMode="contain"
          source={{uri: 'https://api.example.com/images/' + item.image}}
          onLoadEnd={e => this.setState({imageloading: false})}
        />
        {this.state.imageloading && (
          <Image
            style={styles.imageFood}
            resizeMode="contain"
            source={loaderImage}
          />
        )}
        <Text>Some Product name</Text>
      </TouchableOpacity>
    );
  }

  const styles = StyleSheet.create({
    imageFood: {
      width: width / 2.5 - 20 - 10,
      height: width / 2.5 - 20 - 30,
      backgroundColor: 'transparent',
      position: 'absolute',
      top: -45,
    },
    divFood: {
      width: width / 2 - 20,
      padding: 10,
      borderRadius: 10,
      marginTop: 55,
      marginBottom: 5,
      marginLeft: 10,
      alignItems: 'center',
      elevation: 8,
      shadowOpacity: 0.3,
      shadowRadius: 50,
      backgroundColor: 'white',
    },
  });

【讨论】:

    【解决方案2】:

    试试这个:

    class YourComponent extends React.Component {
      render() {
        return data.map(({ img_path }) => (
          <Image
            style={{ height: 212 / 3, width: 300 / 3, borderRadius: 5 }}
            source={{ uri: img_path }}
            loadingIndicatorSource={{
              uri: require('../../../../assets/flower-loader.gif'),
            }}
          />
        ));
      }
    }
    
    }
    

    或者这个

    class YourComponent extends React.Component {
      state = {
        imgsLoaded: data.map(() => false),
      };
    
      componentDidMount() {
        for (let i = 0; i < data.length; i++) {
          Image.prefetch(data[i].img_path, () => {
            const { imgsLoaded } = this.state;
            const imgsLoadedUpdated = imgsLoaded.slice();
            imgsLoadedUpdated[i] = true;
            this.setState({ imgsLoaded: imgsLoadedUpdated });
          });
        }
      }
    
      render() {
        const { imgsLoaded } = this.state;
    
        return data.map(({ img_path }, index) => (
          <Image
            source={{
              uri: imgsLoaded[index]
                ? img_path
                : require('../../../../assets/flower-loader.gif'),
            }}
          />
        ));
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多