gpt4 book ai didi

javascript - 在 react native 应用程序中保留 Firebase 数据

转载 作者:行者123 更新时间:2023-12-03 01:57:05 27 4
gpt4 key购买 nike

我正在尝试在react-native上构建我的第一个应用程序,但我遇到了问题。我想显示来自 firebase 的数据,并且当我重新加载应用程序时插入到 firebase 的数据不会被删除。这就是我的渲染中的内容:

 let tags = this.state.tagArray.map((val, key) => {
return <TagContainer key={key} keyval={key} val={val}
deleteMethod={() => this.deleteTag(key)} />
});

这是我的 firebase 配置:

this.itemRef=this.getRef().child('tags');
getRef(){
return firebaseApp.database().ref();
}

getItems(itemRef){
itemRef.on('value',(snap)=>{
let items=[];
snap.forEach((child) => {
items.push({
title:child.val().title,
_key:child.key
});
});
Array.prototype.push.apply(this.state.tagArray, items);
this.setState({ tagArray: this.state.tagArray })
});

这是我设置 tagArray 的 addtag 函数:

addTag() {
if (this.state.tagText) {
this.state.tagArray.push({
'tag': this.state.tagText
});
this.setState({ tagArray: this.state.tagArray })
this.setState({ tagText: '' })
this.itemRef.push({title:this.state.tagText});
}
}

最佳答案

您不应该将数据推送到状态对象中。如果不使用 setState(),则不允许更改状态。

addTag() {
if (this.state.tagText) {

let tagArray = [...this.state.tagArray,{'tag': this.state.tagText}]
this.setState({ tagArray: tagArray,tagText: '' }, ()=>{
this.itemRef.push({title:this.state.tagText});
})
}
}

关于javascript - 在 react native 应用程序中保留 Firebase 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com