Search code examples
javascriptreactjsreact-nativereduxcomponents

React Native - How to update my component after ternary function?


How can I update my element after return a function? For example:

myFunction(condition){
  if(condition){
      return(
         <Text>True</Text>
      )
   }
   return(
      <Text>False</Text>
   )
}

render(){
   return(){
     <View>
       {this.myFunction(condition)}
     </View>
   }
}

I have the same case, but the element is updated once Even calling it other times the component is not updated.

Obs.: The condition is changing by other functions.

Thanks!!


Solution

  • How about make component like

    changeCondition(condition){
      this.setState({condition})
    }
    
    render(){
         return(
             <View>
                <Text>{this.state.condition ? "True" : "False"}</Text>
             </View>
         )
    }
    

    This is more complying with react rules