RN性能优化之Navigator卡顿优化

2017-06-10  本文已影响0人  哪吒闹海全靠浪

前言

目前RN的导航有Navigator和NavigatorIOS两种。
主要的区别在于NavigatorIOS使用了iOS中的UINavigationController类,是原生的,而Navigator则完全用js重写了一个类似功能的React组件,是js的。因此Navigator的性能不是很好,但可以兼容iOS和Android,而NavigatorIOS的性能比Navigator好很多,但只能用于iOS。
需要两个平台都能用,而且统一,只能用Navigator,同时我们就也要解决它带来的卡顿的问题。

解决方法

具体的代码片段如下:

constructor(props){
        super(props);
        this.state = {
            didMount: false
        };
}
render(){
        const rowData = this.props.rowData;
        return(
            <View style={[styles.container, {backgroundColor: this.props.pageBackgroundColor}]}>
                <View style={styles.contentContainer} {...this._panResponder.panHandlers}>
                    {this.state.didMount ?
                        <WebView
                            ref={(ref)=>{this.webView = ref}}
                            style={[styles.webView, {backgroundColor: this.props.pageBackgroundColor}]}
                            source={{uri: rowData.url}}
                            renderLoading={this._renderLoading.bind(this)}
                            renderError={this._renderError.bind(this)}
                            startInLoadingState={true}
                        />
                        :
                        null
                    }
              </View>
          </View>
)
}
componentDidMount(){
        InteractionManager.runAfterInteractions(() => {
            this.setState({
                didMount: true
            });
        });
    }

参考

http://reactnative.cn/docs/0.22/performance.html#content

上一篇 下一篇

猜你喜欢

热点阅读