Search code examples
javascriptreactjsreact-nativereact-props

How to get Text component value onPress in react-native


I'm a new react-native developer. I want to get the value of Text component using onPress and pass it to a function.

<Text onPress={()=>this.display}>Hello World</Text>


display= (text) =>{
    console.log(text);//this should print Hello world
  }

Solution

  • This method can be used to get text from a text onPress event

     <Text onPress={(event) => console.log(event._dispatchInstances.memoizedProps.children)} >{value}</Text>