You should add a value attribute to the button component and either use hooks or state to set the value and access it throughout the function/class.
Adding value attribute to the button:
<button value="someVal" onClick={handleClick}>Test</button>
Create and store button value using hooks
const [btn,btnVal] = useState("")
handleClick
const handleClick =(event) =>{
const getSameValue = event.currentTarget.value
btnVal(getSameValue)
}
For class-based components:
export default class App extends React.Component {
//const [count, setCount] = useState("");
constructor(){
super()
this.state={
val:""
}
}
handleClick = (event) =>{
this.setState({val:event.currentTarget.value})
}
render(){
return (
<div>
<p>Button value: {this.state.val}</p>
<button value="abcd" onClick={this.handleClick}>
Click me
</button>
</div>
);
}
}
Further, you could access the button's value directly using the state or hook variable.
valueattribute.