Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listing 7.1 handleRadio is incorrect #30

Open
jtcmedia opened this issue Nov 10, 2018 · 0 comments
Open

Listing 7.1 handleRadio is incorrect #30

jtcmedia opened this issue Nov 10, 2018 · 0 comments

Comments

@jtcmedia
Copy link

Currently, handleRadio(event) looks like this:

let obj = {}
obj[event.target.value] = event.target.checked // true
this.setState({radioGroup: obj})

This won't work as written. Since obj is an empty object, the radio buttons in the group besides the one checked will have a value of undefined after this.setState(). The handler should be written as:

let obj = Object.assign(this.state.radioGroup) // prevents undefined
/* iterate through object and set all to false since only one radio 
can be checked at a time.*/
for (const radio in obj) { obj[radio] = false }
obj[event.target.value] = true
this.setState({radioGroup: obj})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant