reactjs - React Radio Button event on "checked" attribute -


i'm trying handle event based on 'checked' attribute of radio button in react.js. want buttons allow selecting more 1 value, removed 'name' attribute seemed disallow multiple selections.

the base radio button component looks

export function radio_button_multiple_selection (props) {   return (     <label>       <input  type="radio"               checked={props.form_data[props.field_name].indexof(props.btn_value) > -1}               onclick={props.radio_effect} />       {props.btn_value}     </label>   ) } 

i have form_data objec has array of values correspond btn_value of radio buttons, if value found in array should come checked. (and part working fine right now, in fact show checked expected):

const form_data = { ...   occupations: ['student', 'merchant', 'paladin', 'troll'], ... } 

now, have react class method manipulating values in occupations array,responding whether radio button being licked checked or not:

handleradiobutton (event) {   const target = event.target;   const value = target.value;   const ischecked = target.checked;   console.log(ischecked) // undefined. why?!?   if (ischecked) {     // remove array     // etc.   } else {     // add array     // etc.   } } 

my main issue following:

when console.log "checked" logic returns boolean inside radiobutton component's checked attribute, comes expected (true of false if or isnt in array). comes checked = undefined in class method handling buttons.

you cannot uncheck radio button in html either. have control checked attribute props or state:

even more, should rely on state, instead of mix, e.target.checked & state.

class radio extends component {   state = {}    render() {     return <input       type="radio"       checked={this.state.checked}       onclick={e => this.setstate({         checked: !this.state.checked       })}     />   } } 

<input type="radio" />


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -