首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-native-video示例抛出"TypeError:undefined“错误

react-native-video示例抛出"TypeError:undefined“错误
EN

Stack Overflow用户
提问于 2020-04-16 18:46:06
回答 1查看 67关注 0票数 0

我是react native的新手。我正在尝试制作一个嵌入了youtube视频的应用程序。我正在尝试这个repo的例子,但它在我的项目中崩溃了。

这是我的完整代码。崩溃在它们被抛出的地方被注释。我不明白我做错了什么。

我正在使用webstorm,我非常确定我已经安装了这个包,并使用thoose命令将其链接起来:

npm安装--保存react-native-video

react-本地链接react-native-video

代码语言:javascript
复制
import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';
import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import Video from 'react-native-video';

const App: () => React$Node = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <View style={styles.body}>
            <View>
              <Video source={{uri: "https://www.youtube.com/watch?v=83mkGuGLNZg"}}
                 ref={(ref) => {
                   this.player = ref //Crash with TypeError:undefined is not an object
                 }}                                      // Store reference
                 onBuffer={this.onBuffer}                //Crash with TypeError:undefined is not an object
                 onError={this.videoError}               //Crash with TypeError:undefined is not an object
                 style={styles.backgroundVideo}
              />
            </View>
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
  backgroundVideo: {
    position: 'absolute',
    top: 0,
    left: 0,
    bottom: 0,
    right: 0,
  },
});

export default App;
EN

回答 1

Stack Overflow用户

发布于 2020-04-16 18:59:02

问题出在您的this语句上。它指的是你所处的功能。

当使用严格模式时,函数中的‘

’将引用它的显式值,如果‘this’没有显式设置,则‘this’将是未定义的。

所以你必须用this.player=this.player.bind(this)bind它。

有关更多信息,请查看here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61248351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档