In vanilla Javascript I can access 'this' keyword in event listeners like this:
<body>
<button type="button" onclick="test(this)">Click Me</button>
<script>
function test(t) {
console.log(t); //this
}
</script>
</body>
or like this
<body>
<button id="test" type="button">Click Me</button>
<script>
document.getElementById("test").onclick = function () {
console.log(this);
};
</script>
</body>
But how do i access DOM element using 'this' keyword in react event listener
export default function Button() {
function test() {
console.log(this); //undefined
}
return (
<button onClick={test} id="test" type="button">
Click Me
</button>
);
}
I know I can access the target element using 'event' object. But I'm curious whether it is possible to use 'this' in react event listener