RN错误之undefined is not a function

写项目的时候碰到this对象错误,具体报错为

error

解决办法.
原因是this对象和state的this对象不是同一个所致。将其修改为同一个就可以了

constructor(props){
        super(props);
        this.state={
            indexPage:0,
        };
    }
    render() {
        return (
            <View style={styles.container}>
                {/*上面的滚动部分*/}
                <ScrollView
                    pagingEnabled={true}
                    showsHorizontalScrollIndicator={false}
                    horizontal={true}
                    onMomentumScrollEnd = {this.onScrollAnimationEnd}
                >
               
                 {this.renderTopScrollItem()}   
                </ScrollView>

              
            </View>
        );
    }
    //当滚动动画结束之后调用此回调
    //如果出于某些原因想使用浏览器原生事件,可以使用 nativeEvent 属性获取
    //contentOffset 用来手动设置初始的滚动坐标
    onScrollAnimationEnd(e){
        var currPage = Math.floor(e.nativeEvent.contentOffset.x/width);

          this.setState({
            indexPage:currPage
        });

    }

修改

 constructor(props){
        super(props);
         self = this,// self = this;//为了防止this.setState的this对象不一致
        self.state={
            indexPage:0,
        };
    }
    render() {
        return (
            <View style={styles.container}>
                {/*上面的滚动部分*/}
                <ScrollView
                    pagingEnabled={true}
                    showsHorizontalScrollIndicator={false}
                    horizontal={true}
                    onMomentumScrollEnd = {this.onScrollAnimationEnd}
                >
               
                 {this.renderTopScrollItem()}   
                </ScrollView>
             </View>
        );
    }
    //当滚动动画结束之后调用此回调
    //如果出于某些原因想使用浏览器原生事件,可以使用 nativeEvent 属性获取
    //contentOffset 用来手动设置初始的滚动坐标
    onScrollAnimationEnd(e){
        var currPage = Math.floor(e.nativeEvent.contentOffset.x/width);

          self.setState({//和state的this一致
            indexPage:currPage
        });

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,786评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,027评论 19 139
  • 原文地址note.youdao.com/web/#/file/recent/note/WEBecc734b7686...
    白鹿猿阅读 284评论 1 9
  • 郎永淳,这个名字对于工作后就不看新闻联播的我确实是陌生的,因为下班回到家的时间早已错过那个点。因此哪怕有好几年他天...
    窗外阳光阅读 674评论 12 20