【发布时间】:2017-05-26 18:03:35
【问题描述】:
每当我的互联网连接不好时,我都会不断收到此警告(我不确定这是不是发生这种情况的正确原因)。 我在反应本机应用程序(iOS/android)中显示来自 Firebase 数据库的数据的页面中得到了这个。 PS:我不考虑制作离线模式(这不是必需的)。 我正在与您分享我在其中一个页面中使用的代码(在所有页面中都非常相似)
代码
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>
);
}
}
它确实提到了组件的名称,但我仍然不知道这可能是什么原因
更新:我尝试了这个建议的解决方案:
componentWillUnmount() {
this.itemRef.off();
}
事实上,我有一个后退按钮,可以从一个页面更改为另一个页面(每个页面都有自己的路径和从 Firebase 提取的数据,但代码相似),我收到此错误:this.itemRef 在每个页面中都不同。
但我收到此错误:
任何想法或建议..谢谢你
【问题讨论】:
标签: javascript firebase react-native try-catch warnings