1

I'm new to react-native and trying to build a simple app with firebase as backend.

I have made a sign-up, login, logout and setting a mobilenumber. I'm trying to read the mobilenumber data realtime. The function to get the data is as follow:

    _listenForMobileNumberChanges(userId, callback) {

    let userMobilePath = "/user/" + userId + "/details";

    firebase.database().ref(userMobilePath).on('value', (snapshot) => {

        var mobile = "";

        if (snapshot.val()) {
            mobile = snapshot.val().mobile
        }

        callback(mobile)
      });
    }

In the render function I placed a ListView and called the function:

<ListView
        dataSource={this.state.mobile}
        renderRow={() => {this._listenForMobileNumberChanges(this.state.userId}}
     />

I know i'm missing the callback parameter in the Listview call, but I don't know what to add to the parameter, could anyone help?

1 Answer 1

2

Instead of using a Listview use TextInput instead and in the onChangeText attribute for the TextInput make a call to the metode like this:

    <TextInput
      style={{height: 40, borderColor: 'gray', borderWidth: 1}}
      onChangeText={(usermobileNumber) => this._listenForMobileNumberChanges()}
      value={this.state.usermobileNumber}
    />

Parse no parameters just modify the methode to this:

_listenForMobileNumberChanges() {
    alert('inside the listen function');

    if(firebase.auth().currentUser) {
      let userId = firebase.auth().currentUser.uid;
      let userMobilePath = "/user/" + userId + "/details";

      firebase.database().ref(userMobilePath).on('value', (snapshot) => {

          var mobile = "";

          if (snapshot.val()) {
              mobile = snapshot.val().mobile
          }

          this.setState ({usermobileNumber: mobile});
      });
    } else {
      this.setState ({usermobileNumber: ''});
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.