Skip to content

Listing 7.1 handleRadio is incorrect #30

@jtcmedia

Description

@jtcmedia

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})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions