美文网首页react native
RN错误之undefined is not a function

RN错误之undefined is not a function

作者: 飞奔的小马 | 来源:发表于2017-03-13 10:23 被阅读296次

    写项目的时候碰到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
            });
    
        }
    

    相关文章

      网友评论

        本文标题:RN错误之undefined is not a function

        本文链接:https://www.haomeiwen.com/subject/bwtyittx.html