1

我正在获取数据,然后将它们保存在移动设备上的 sqlite 文件中。当这个屏幕被调用时,获取代码开始被componentDidMount()函数调用。当提取结束时,这会setState()调用componenDidUpdate()函数(将数据保存在 sqlite then 中setState= 'isLoadingDataProgramadas: false'),所以我希望应用程序'ActProgramadas'使用 React-Navigation V3 转换到屏幕。下面的解决方案有效,但给我一个警告:

警告:在现有状态期间无法更新(例如在“渲染”内)。渲染方法应该是 props 和 state 的纯函数。

我该如何解决?

componentDidMount() {
    this.doFetch();
 }

componentDidUpdate(){
    this.openBD();
    this.insertSQLITE();
}
 componentWillUnmount() {
    this.closeDatabase();
 }

 render() {
    if(this.state.isLoadingDataProgramadas)
      return (
          <View style={stylesLoading.container}>
              <View>
                  <ActivityIndicator size="large" color="lightblue"/>
              </View>

              <View>
                  <Text style={stylesLoading.texto}>
                  Descargando datos... 
                  </Text>
              </View>
          </View>
      );
    else
      return( 
         <View>
           {this.props.navigation.navigate('ActProgramadas')}
         </View>

      );
  }
4

2 回答 2

0

可能 navigation.navigate 在后台使用了一个 setState 函数。我的建议是制作一个函数来将导航封装到这样的 setTimeout 函数中

navigate = () => {
    const { navigation } = this.props
    setTimeout(() => {
        navigation.navigate('ActProgramadas')
    }, 1000);
}

...

<View>
    {this.navigate()}
</View>
于 2019-05-13T14:49:24.123 回答
0
  1. 它看起来像您的if else陈述中的错字。
  2. 并尝试像这样绑定您的导航事件处理程序或在构造函数中执行此操作。

尝试这个:

render() {
    if(this.state.isLoadingDataProgramadas) {
      return (
          <View style={stylesLoading.container}>
              <View>
                  <ActivityIndicator size="large" color="lightblue"/>
              </View>

              <View>
                  <Text style={stylesLoading.texto}>
                  Descargando datos... 
                  </Text>
              </View>
          </View>
      );
      } else {
      return( 
         <View>
           {() => this.props.navigation.navigate('ActProgramadas')}
         </View>

      );
      }
  }
于 2019-05-13T14:43:40.563 回答