react-native - onLoadEnd 未在 native react 中触发

标签 react-native events onload-event

我正在尝试在 react native 上显示来自 giphy api 的 GIF。
Gif 需要时间在屏幕上显示,所以我想在中间显示一个微调器。
onLoadEnd 事件似乎永远不会在 Image 标签上触发,因此微调器实际上无休止地运行,因为我永远无法在我的状态下更新加载。我在这里做错了什么?

import React, { Component } from 'react';
import { View, Text, ScrollView, Image} from 'react-native';
import axios from 'axios';
import QuoteDetail from './quote_detail'
import Spinner from './spinner'

// class based component knows when it's gona be rendered
class QuoteList extends Component {
  state = { quotes: [],
            giphyUrl: 'https://media.giphy.com/media/nZQIwSpCXFweQ/giphy.gif',
            loading: true
          };

  componentWillMount() {
    console.log('Again?')
    axios.get('https://api.tronalddump.io/search/quote?query='+this.props.characterName)
      .then(response => this.setState({ quotes: response.data._embedded.quotes }))
    this.getGiphy()
  }

  getGiphy() {
    console.log('getgif')
    const GiphyUrl = "https://api.giphy.com/v1/gifs/search?api_key=mBJvMPan15t7TeLs8qpyEZ7Glr5DUmgP&limit=1&q=" + this.props.characterName.replace(" ", "+");
    console.log(GiphyUrl)
    axios.get(GiphyUrl)
      .then(response => {
                  console.log(response)
                  console.log(response.data.data[0].url)

                  this.setState({ giphyUrl: response.data.data[0].images.original.url})
                  console.log(this.state)
                })

  }

  renderQuotes() {
    return this.state.quotes.map(
      quote => <QuoteDetail key={quote.quote_id} quote={quote}/>
    );
  }



  render() {
    if (this.state.loading) {
      return < Spinner />;
    }
    else {
      return (
        <ScrollView>
          <View style={styles.viewStyle}>
            <Image
              source={{uri: this.state.giphyUrl}}
              key={this.state.giphyUrl}
              style={styles.gifStyle}
              onLoadEnd={() => console.log('im done loading')}
            />
            <Text style={styles.vsStyle}>VS</Text>
            <Image
              source={{ uri: "https://media.giphy.com/media/xTiTnHXbRoaZ1B1Mo8/giphy.gif"}}
              key="https://media.giphy.com/media/xTiTnHXbRoaZ1B1Mo8/giphy.gif"
              style={styles.gifStyle}
            />
          </View>
          {this.renderQuotes()}
        </ScrollView>
      );
    }
  }
}

const styles = {
  gifStyle: {
    height: 200,
    width: 190
  },
  viewStyle: {
    flexDirection: 'row'
  },
  vsStyle: {
    marginTop: 95,
    marginLeft: 5,
    marginRight: 5,
    fontWeight: 'bold',
    fontSize: 20
  }
};

export 

默认报价单;

最佳答案

这里似乎错误的一件事是 getGiphy 和 renderQuotes 属性不是箭头函数或不在构造函数中绑定(bind)“this”。
根据您的代码判断,您不能使用 setState 函数导致错误的“this”范围。

试试这个,看看这是否有效

getGiphy = () => {...}
renderQuotes = () => {...}

您确定没有收到“setState 未定义”之类的错误消息吗?

关于react-native - onLoadEnd 未在 native react 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50188373/

相关文章:

events - GWT 的 BrowserEvent 和 NativeEvent 到底是什么?

.net - 观察者模式与事件驱动模型有何不同?

javascript - 在 Javascript 中处理多个关键事件的最佳方式是什么?

javascript - 将 Window Onload 函数转换为非阻塞

javascript - Hook javascript 事件来加载页面

meteor - 我可以将 wix/react-native-navigation 创建的根组件包装在 ApolloProvider 组件中吗

react-native - 如何在 React Native 第一次运行时启动远程调试

swift - 我怎样才能从 native react 中获取值(value)到我的桥梁快速类(class)中?

reactjs - 使用 Flow 和 redux-navigation 验证通用 React 组件属性

c# - 永远不会调用主窗体的 OnLoad 覆盖