How to use radio buttons in ReactJS?

前端 未结 10 2159
再見小時候
再見小時候 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:26

    I also got confused in radio, checkbox implementation. What we need is, listen change event of the radio, and then set the state. I have made small example of gender selection.

    /*
     * A simple React component
     */
    class App extends React.Component {
      constructor(params) {
         super(params) 
         // initial gender state set from props
         this.state = {
           gender: this.props.gender
         }
         this.setGender = this.setGender.bind(this)
      }
      
      setGender(e) {
        this.setState({
          gender: e.target.value
        })
      }
      
      render() {
        const {gender} = this.state
        return  
    Gender:
    Male Female
    { "Select Gender: " } {gender}
    ; } } /* * Render the above component into the div#app */ ReactDOM.render(, document.getElementById('app'));
    
    
    

提交回复
热议问题