Search code examples
javascriptreactjsreact-bootstrap4-modal

How to clear radio buttons / clear all selected radio buttons to false state. reactjs - bootstrap


How can I reset radio button to default state or clear all selected radio buttons. CODE


Solution

  • You could implement your onReset method in this way to clear (or reset) all selected radio buttons in your form:

    onReset = () => {
      // Extract out cards from your state
      var cards = this.state.cards
    
      // Iterate each card..
      cards.forEach(card => {
    
        // Iterate each card option..
        card.options.forEach(option => {
    
          // Reset selected value of option
          option.selected = false
        })
      })
    
      // Apply "reset" card state to your component to cause a re-render
      this.setState({ cards : cards })
    }
    

    Here is a running sample for you to see in action: https://stackblitz.com/edit/react-ujmlcz?file=index.js