I'm new to React so apologies if this is a simple/silly question. I'm trying to create a simple calculator app using React and I'm trying to simply print the value of a button I click. I've got a keypad component which will render all the buttions, but for now I only have one.
const Keypad = () => {
return (
<div className="Keypad">
<button name='1' onClick={e => this.onClick(e.name)}>1</button>
</div>
)
}
export default Keypad
In my main App file I've got an onClick method which will check the value of the button pressed and print it and when I call the Keypad component I call onClick as well:
const onClick = (e) => {
if (e.name === "1") {
console.log(e.name)
} else {
console.log("nope")
}
};
return (
<div className="Header">
<header>
<h1>Calculator</h1>
</header>
<Result />
<Keypad onClick={onClick}/>
</div>
);
It compiles successfully, but when I try and click the button I get a "TypeError: Cannot read property 'onClick' of undefined" error. I've followed some steps from different websites and am confused as to why this doesnt work..