React-Native:显示加载屏幕,直到加载webview

Nin*_*yHH 12 android webview reactjs react-native

我现在有一个组件SplashScreen,我首先渲染,直到我的状态设置.我想以某种方式找到一种方法如何在我的webview加载时仍然显示这个组件.我将onLoadEnd添加到我的webview中,看起来我在完成加载时收到了我的消息,问题是如果我首先加载启动画面并等待状态被更改onLoadEnd实际上永远不会被更改因为webview还没有渲染.有一个很好的方法怎么做?

Nin*_*yHH 35

我的解决方案实际上很简单,WebView组件可以有param renderLoading,这对我来说不起作用,我想出来是因为还需要定义startInLoadingState.

所以我的WebView看起来像这样:

<WebView
   ref={MY_REF}
   source={source}
   renderLoading={this.renderLoading}
   startInLoadingState
/>
Run Code Online (Sandbox Code Playgroud)

  • 这对我帮助很大.奇迹般有效.对于renderLoading,你甚至可以使用箭头函数:`renderLoading = {()=> {return(<ActivityIndi​​cator />}`但这对我来说也有一点Bug.如果你通过箭头加载指标 - Funktion它被放置使用默认本地函数:`function renderLoading(){return(<ActivityIndi​​cator />);}`并像你描述的那样加载它,它位于屏幕中间位置很好.我不知道为什么,但也许这可以帮助别人节省一些时间:) (2认同)
  • 谢谢,这是要走的路,或者作为suther评论的`renderLoading = {()=> {return(<ActivityIndi​​cator />)}`但是加载器位置与你如何声明或调用你的函数无关!如果你没有设置ActivityIndi​​cator组件的样式,它将只显示在中心的顶部.要使ti居中verticaly也添加样式:`<ActivityIndi​​cator style = {{position:'absolute',left:0,right:0,top:0,bottom:0,alignItems:'center',justifyContent:'center "}} />` (2认同)

Jan*_*ang 11

这将是我的方法:

constructor(props){
    super(props);
    this.state = { webviewLoaded: false };
}

_onLoadEnd() {
    this.setState({ webviewLoaded: true });
}

render() {
    return(
        <View>
            {(this.state.webviewLoaded) ? null : <SplashScreen />}
            <WebView onLoadEnd={this._onLoadEnd.bind(this)} />
        </View>
    )
}
Run Code Online (Sandbox Code Playgroud)

这样,webview就会被渲染,但它被放置在后面SplashScreen.因此webview SplashScreen在显示时开始加载.