How to use radio buttons in ReactJS?

前端 未结 10 2172
再見小時候
再見小時候 2020-11-27 10:05

I am new to ReactJS, sorry if this sounds off. I have a component that creates several table rows according to the received data.

Each cell within the column has a

10条回答
  •  广开言路
    2020-11-27 10:40

    import React, { Component } from "react";
    
    class RadionButtons extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
          // gender : "" , // use this one if you don't wanna any default value for gender
          gender: "male" // we are using this state to store the value of the radio button and also use to display the active radio button
        };
    
        this.handleRadioChange = this.handleRadioChange.bind(this);  // we require access to the state of component so we have to bind our function 
      }
    
      // this function is called whenever you change the radion button 
      handleRadioChange(event) {
          // set the new value of checked radion button to state using setState function which is async funtion
        this.setState({
          gender: event.target.value
        });
      }
    
    
      render() {
        return (
          
    Male
    Female
    ); } } export default RadionButtons;

提交回复
热议问题