简体   繁体   中英

Firebase + React - Not changing my state

I have this block of code:

componentDidMount() {
    const db = firebase.database();
    let dbRefRoot = db.ref().child('sponnsor');

    dbRefRoot.child('test').on('value', snap => {
        let userData = snap.val();
        console.log('test', userData );

        this.setState({
            test: snap.val()
        })
    })

}

And I just would like be sure that is working and I'm going on right direction! Because every time that I update my information on Firebase, nothing happen! I was expecting see my console log working! It isn't, and I cant understand why.

ps: Im importing: import * as firebase from 'firebase';

My obj:

 { "sponnsor": { "test": 1234 } } 

Now that you have published your object structure I think you should do as:

let dbRefRoot = db.ref().child('sponnsor');

dbRefRoot.on('value', snap => {
    let sponnsorData = snap.val();
    console.log('test', sponnsorData.test );

    this.setState({
        test: sponnsorData.test
    })
})

Updating is very simple like this

firebase.database().ref('sponnsor')
.update({test:3456})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM