当我第一次进入此屏幕时,Firebase会显示每个项目。当我转到另一个屏幕并返回此屏幕时,Firebase仅显示最后一项。我不知道哪里有错误以及如何解决。
var data = []
var firebaseConfig = {
apiKey: "key",
authDomain: "information",
databaseURL: "information",
projectId: "information",
storageBucket: "information",
messagingSenderId: "information",
appId: "information"
};
firebase.initializeApp(firebaseConfig);
export default class ConfScreen extends React.Component {
constructor(props){
super(props);
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2})
this.state = {
listViewData: data,
newMarker: "",
}
}
componentDidMount(){
var that = this
firebase.database().ref('/markers').on('child_added', function(data){
var newData = [...that.state.listViewData]
newData.push(data)
that.setState({listViewData: newData})
})
}
<List
enableEmptySections
dataSource={this.ds.cloneWithRows(this.state.listViewData)}
renderRow={data =>
<ListItem>
<Text>{data.val().title}</Text>
</ListItem>
} />
答案 0 :(得分:0)
我不确定问题出在哪里,但这更有可能解决问题:
firebase.database().ref('/markers').on('value', function(snapshot){
var newData = [...that.state.listViewData]
snapshot.forEach(function(data) {
newData.push(data);
});
that.setState({listViewData: newData})
})