React Native element onPress get button title
React Native element Button
In React Native, you can use the onPress
prop on a button element to specify a function that will be called when the button is pressed. To access the button’s title in this function, you can pass the title as a prop to the button element and then access it inside the onPress
function.
Example:
<Button
title="My Button"
onPress={() => {
console.log(this.props.title);
}}
/>
You can also use the arrow function, and event
object to get the title of the button.
<Button
title="My Button"
onPress={(event) => {
console.log(event.target.title);
}}
/>
You can also use event.currentTarget.innerText
to get the title of the button.
<Button
title="My Button"
onPress={(event) => {
console.log(event.currentTarget.innerText);
}}
/>