Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio buttons need to click twice to reflect change

1.whenever i click on radio button it call the handleOptionChange callback but not reflect the change in html. 2.but when i click twice on radio button it reflect the change

My question is why i need to click twice on radio buttons? Is am i doing anything wrong?

class CompleteProfile extends Component{

    constructor(props){
        super(props);
        this.state = {
            teacher_role:'subject'
        };
        this.handleOptionChange = this.handleOptionChange.bind(this);
    }

    handleOptionChange(event){
        event.preventDefault();
        let { name, value } = event.target;
        this.setState( { [name]:value } );
    }

    render(){
        let { teacher_role } = this.state;
        console.log('teacher:',teacher_role);
        return(
            <Wrapper className="complete-profile">
                <CustomContainer>
                    <Title>complete profile</Title>
                    <ProfileCard>
                        <Form noValidate>

                            <FormGroup>
                                <label>I want to enroll as:</label>

                                <label htmlFor="test">
                                    <input id="test" type="radio" name="teacher_role" value="subject" checked={ teacher_role === 'subject' } onChange={ this.handleOptionChange } />
                                    subject teacher
                                </label>

                                <label htmlFor="test1">
                                    <input id="test1" type="radio" name="teacher_role" value="chapter" checked={ teacher_role === 'chapter' } onChange={ this.handleOptionChange } />
                                    class teacher
                                </label>

                                <label htmlFor="test2">
                                    <input id="test2" type="radio" name="teacher_role" value="both" checked={ teacher_role === 'both' } onChange={ this.handleOptionChange } />
                                    both
                                </label>

                            </FormGroup>
                        </Form>
                    </ProfileCard>
                </CustomContainer>
            </Wrapper>
        )
    }

    componentWillUpdate(nextProps){

    }

    componentDidUpdate(prevProps){

    }

}
like image 946
Prakash Naidu Avatar asked Jan 24 '18 12:01

Prakash Naidu


People also ask

Are radio buttons multi select?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name.

How can I get radio button to click?

To check which radio button is selected in a form, we first get the desired input group with the type of input as an option and then the value of this selection can then be accessed by the val() method. This returns the name of the option that is currently selected.

How do you focus radio buttons?

Input Reset focus() Method The focus() method is used to give focus to a radio button. Tip: Use the blur() method to remove focus from a radio button.


Video Answer


3 Answers

remove event.preventDefault(); from your handleOptionChange method

like image 78
Trevor Atlas Avatar answered Oct 09 '22 02:10

Trevor Atlas


For others running into this problem, make sure you are not using the same name property for multiple groups of radio buttons.

This can happen when mapping through an array and rendering a set of radio buttons for each array item. Avoid this issue by adding the index of the current array item to the radio button name.

Note that in this case there should be no need for preventDefault in the handleOptionChange method.

like image 22
Lurifaxel Avatar answered Oct 09 '22 01:10

Lurifaxel


If you are like me and need to use event.preventDefault(), for example to prevent the input from submitting. You can use event.stopPropagation() instead.

see this issue comment explaining for more context

like image 5
Tomiwa Avatar answered Oct 09 '22 01:10

Tomiwa