I have an interesting question regarding React state and onChange events in the form. In my React component, I am storing information in the state, that includes objects. When a user types in a change, I would like part of that object in the state to change. As an example, here is the state I am talking about:
this.state = {
total: 0,
email: '',
creditCards: [],
selectedCreditCard: {},
billingAddresses: [],
shippingAddresses: [],
selectedBillingAddress: {},
selectedShippingAddress: {},
}
This is the state of the component, and this is my handle change function:
handleChange(event) {
this.setState({
[event.target.name]: event.target.value,
})
}
As you can probably tell, this type of function will not work for objects within the state. As an example, when a user types in their credit card number, I would like this.state.selectedCreditCard.creditCardNumber to be the thing that changes. Of course, with the way it is setup in my form:
<input
type="text"
name="selectedCreditCard.creditCardNumber"
value={selectedCreditCard.creditCardNumber}
onChange={(evt) => handleChange(evt)}
/>
The "name" being passed into the onChange is only a string, and not a pointer to a key value in one of the objects in the state. Therefore, there is no easy way for this function to work except for deconstructing each object in the state and placing every key:value directly in the state instead of within the object; doing this is a possibility, but the code becomes very cumbersome this way. I hope all of this makes sense. Is there any way to manipulate the objects within the state, and have the form pass a pointer to a key in the object, as opposed to a string? Thanks.