2017-05-26 63 views
1

当我的网络连接不好时,我总是收到此警告(我不确定这是否是正确的原因)。 我在显示来自反应原生应用程序(iOS/android)中的Firebase数据库中的数据的页面中获得了此信息。 PS:我不打算做离线模式(这不是要求)。 我与你分享我正在使用的代码在这些网页之一(它在所有页面非常相似)setState:只能更新已安装或挂载的组件反应原生/未定义不是对象

代码

constructor(props) { 
      super(props); 
      this.state = { 
       dataSource: new ListView.DataSource({ 
        rowHasChanged: (row1, row2) => row1 !== row2, 
       }) 
      }; 

      this.itemsRef = this.getRef().child('path'); 

     } 

     getRef() { 
      return firebaseApp.database().ref(); 
     } 
     componentDidMount() { 
      this.listenForItems(this.itemsRef); 
     } 
     listenForItems(itemsRef) { 

      itemsRef.on('value', (snap) => { 
       if (snap.val()) { 
        var items = []; 
        snap.forEach((child) => { 
         items.push({ 
          text : child.val().text, 
          title: child.val().title, 
          key: child.key 
         }); 
        }); 

this.setState({dataSource: this.state.dataSource.cloneWithRows(items)}); 
       } 

      }); 

     } 

     render() { 
      return (
       <View> 
        <ListView 
         automaticallyAdjustContentInsets={true} 
         dataSource={this.state.dataSource} 
         renderRow={this._renderItem.bind(this)}/> 
       </View> 

      ); 
     } 

     _renderItem(item) { 
      return (
       <View> 
        <Text>{item.title}</Text> 
        <Text>{item.text}</Text> 
       </View> 
      ); 
     } 
    } 

,这里是我不断收到警告:enter image description here

它确实提到组件的名字,但我仍然不知道什么可能是这个

更新的原因:我想这个建议的解决方案:

componentWillUnmount() { 
     this.itemRef.off(); 
    } 

其实我有一个后退按钮从一个网页变化到另一个(每个页面都有自己的道路,并从火力地堡但类似的代码提取的数据),我得到这个错误:this.itemRef每页都不相同。 但我得到这个错误:enter image description here 任何想法或建议。谢谢

回答

1

当您订阅了on()时的参考数据库路径进行修改,你会得到通知,出现这种情况也时一个组件被卸载。为了防止这个警告,您必须call off()同一个数据库参考上componentWillUnmount

componentWillUnmount() { 
    this.itemsRef.off(); 
} 
+0

谢谢你..我想你的,它是我的另一个错误..请参阅..再次感谢您 – user3521011

+0

我输入了错误的更新,它的'this.itemsRef.off()' – nicokant

+0

ahhh是我没有注意到我自己.. – user3521011

相关问题