【问题标题】:Using arrayUnion causes memory leak使用 arrayUnion 会导致内存泄漏
【发布时间】:2020-07-09 12:57:29
【问题描述】:

我试图将状态数组上传到 Firestore 中的数组,但它发出警告“无法对未安装状态执行更新”。当我去数据库时,我可以看到数据还没有被存储。

const dbh = firebase.firestore();
        this.state.players.map(player => {
        if(player.Present === true){
            this.setState({
                enrolledPlayersList: [...this.state.enrolledPlayersList, player.id]
            })
        }
    })
    const finalList = this.state.enrolledPlayersList
    dbh.collection("Groups").doc(this.state.group)
    .collection('Enrolled').doc('ids').update({
         players: firebase.firestore.FieldValue.arrayUnion(finalList)
    })

【问题讨论】:

  • 你能分享更多你的代码吗?此警告与您对this.state 的使用和更新有关。看看这个github issue,可能对这个有用

标签: react-native google-cloud-firestore


【解决方案1】:

我已通过将与数据库的连接置于 componentDidMount() 函数中来解决此问题,如下所示

componentDidMount(){
const dbh = firebase.firestore();
    this.state.players.map(player => {
    if(player.Present === true){
        this.setState({
            enrolledPlayersList: [...this.state.enrolledPlayersList, player.id]
        })
    }
})
}

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2010-11-18
    • 1970-01-01
    • 2021-03-23
    • 2021-09-25
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多