1

I have a problem about how to print a value,in a function, in react-native.

Basically I have a function that research in the DB some values, and I should print this values.

findUtente(cf) {
    let params = {};
    console.log(cf)
    params = {
      "Person.FiscalCode": cf
    };
    global.utente.db.localdb().find({
        selector: params
      })
      .then(response => {
          let utente = response.docs[0];
          console.log ("utente: " + utente)
          utente.Person.FirstName;
          console.log ("utente.Person.FirstName: " + utente.Person.FirstName)
        })
        //.... catch...}); }
render() {
   {this.findUtente(this.props.cf)}
return (
      <View style={style.container}>
        <View style={style.page}>
          <KeyboardAwareScrollView>
            <Text style={visualProfilo.text}>Name:</Text>
            <Text style={visualProfilo.text1}>{Stamp Here the FirstName}</Text>

The value in the console log is print in the right way. How can I print that value?

Thank you

1 Answer 1

1

You can use componentDidMount to call the method then you can set the value to state and then update value using setState then render state value example:

state = {
   FirstName: ''
}
componentDidMount(){
  this.findUtente(this.props.cf)
}

findUtente(cf) {
    let params = {};
    console.log(cf)
    params = {
      "Person.FiscalCode": cf
    };
    global.utente.db.localdb().find({
        selector: params
      })
      .then(response => {
          let utente = response.docs[0];
          this.setState({FirstName: utente.Person.FirstName})
        })
        //.... catch...}); }
render() {
  return (
   //everything remains same
   <Text style={visualProfilo.text1}>{this.state.FirstName}</Text>
  )
}

Sign up to request clarification or add additional context in comments.

5 Comments

And How can I print the value in the rendere () { return ??
Why do want to print on the render function ??
I would to print the value where I wrote {Stamp Here the FirstName}
It's works!! Thank you so much! (Only last question, I should call componentWillUnmount() ???
No need to call componentWillUnmount.

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.