Reputation: 1694
I am trying to have my state
be set to which of 2 buttons the user presses.
const [pizzaSize, setPizzaSize] = useState(null);
const changePizza = (e) => {
setPizzaSize(e.target.value)
}
<button type="button" onClick={() => changePizza()}>Medium</button>
This is throwing the following error TypeError: Cannot read property 'target' of undefined
I understand that I am not doing this correctly but I can't figure out how to set the state to the the value of the button that is clicked.
My full code is here in case it is needed.
Thank you for your time.
Upvotes: 0
Views: 388
Reputation: 1341
You can even pass the function directly and the event param will b passed automatically to changePizza
<button type="button" value = "Medium" onClick={changePizza}>Medium</button>
Or you can directly do this
<button type="button" value = "Medium" onClick={(e) =>setPizzaSize(e.target.value)}>Medium</button>
Upvotes: 1
Reputation: 61
You have to give value property to your button which is right now set to undefined and pass the event to handler Function
<button type="button" value = "Medium" onClick={(e) => changePizza(e)}>Medium</button>
Upvotes: 2