Cesar Cabral
Cesar Cabral

Reputation: 341

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
	}
}

Upvotes: 1

Views: 50

Answers (2)

Prince Hamza
Prince Hamza

Reputation: 1748

Updating is very simple like this

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

Upvotes: 1

Renaud Tarnec
Renaud Tarnec

Reputation: 83181

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
    })
})

Upvotes: 0

Related Questions