Tuna Gündüz
Tuna Gündüz

Reputation: 17

Cant set state from function

Can't Set State

I'm trying to set my state's randomUserDataName to randomUser.Name that I retrieve from another function But cant set state data from I retrieve getRandomUser function

function getRandomUser() {
  var ref = firebase.database().ref("users");
  ref.limitToLast(1).on(
    //"value",
    "child_added",
    function (snapshot) {
      () => console.log("after this log is current user's data");
      var randomUser = snapshot.val();
      var randomUserDataName = randomUser.Name;
      var randomUserDataLastName = randomUser.LastName;
      var randomUserDataAge = randomUser.Age;
      var randomUserDataBio = randomUser.Bio;
      console.log(randomUserDataName);
      console.log(randomUserDataAge);
    },
    function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    }
  );
}

Here the get user's data function

class HomeScreen extends Component {
  constructor(props) {
    super(props);
    this.state = {
      randomUserDataName: "",
      randomUserDataLastName: "",
      randomUserDataBio: "",
      randomUserDataAge: "",
      randomUserDataInstagram: "",
      currentuserUID: "",
    };
  }

## here the my class's state

Upvotes: 0

Views: 42

Answers (1)

Fábio BC Souza
Fábio BC Souza

Reputation: 1220

You have to use setState. Please better take a look into docs:

https://reactjs.org/docs/react-component.html#setstate

Upvotes: 1

Related Questions