How to read data from a RealTimeDatabase in
Short answer
You must use setState
instead of changing the state directly.
componentDidMount(){
const { uid } = firebaseAuth.currentUser
this.getUserRef().child(uid).once('value',function(snapshot){
this.setState({ dataUser: snapshot.val() })
})
}
Long answer
A key concept in React (and therefore, in React Native as well) is that of immutability .
In particular, the internal state of the React components (the state
) must be immutable , that is, it must not be mutated / modified directly but replaced every time we want to make a change . For this, React provides us with the method setState
(documented here ) that is responsible for updating the same and that generates as a consequence a new render that makes the changes are reflected in the UI.