francol
francol

Reputation: 11

object returning old state

I want to return current state in function, which is returning object.


myFunction = () => {
        return (
            myObject = {
                object: this.state.variable
            }
        )
    }
render() {
   return (

    <TouchableOpacity
       onPress={() => {this.setState({ variable: 2 }),  () => console.log(myFunction())}>

    </TouchableOpacity> )                   
}

Function is returning state, which was default. I want to return new state, which was setted.

Upvotes: 0

Views: 52

Answers (2)

xrequillart
xrequillart

Reputation: 5

Try this one:

render() {
   return (
    <TouchableOpacity
       onPress={() => {
         const value = 2;
         this.setState({ variable: value }); 
         console.log(myFunction(value);
       }
    }></TouchableOpacity>
   )                   
}
myFunction = (value) => {
  return (
    myObject = {
      object: value,
    },
  ),
}

Upvotes: 0

Idan
Idan

Reputation: 4023

Try this one:

render() {
  return (
    <TouchableOpacity onPress={() => {
        this.setState({ variable: 2 },  
        ()=>console.log(this.myFunction()));
    }>

    </TouchableOpacity> )                   
}

Upvotes: 1

Related Questions