简体   繁体   中英

How can i handle an array of checkbox using react.js

I am trying to handle an array of checkbox using react.js. Here is my code:

import React, {Component} from 'react'
export default class Index extends Component{
 state = {
    myArray = ["Chair", "Spoon", "Cup"]
  }
 render(){
  return(
     {
       this.state.myArray.map(() => {
         <FormGroup>
           <Input
             name = {item}
             value = {item}
             checked = 
             onChange = 
         </FormGroup>
        })
     }
   )
 }
}

I am actually new to react and don't know how to handle or manipulate the array.

import React, {Component} from 'react'
export default class Index extends Component{
 state = {
    myArray = ["Chair", "Spoon", "Cup"],
    checked= [false,false,false]
  }
 handleChange=(index)=>{
   let newChecked=this.state.checked
       newChecked=!this.state.checked[index]
  this.setState({checked:newChecked})
} 
 render(){
  return(
     {
       this.state.myArray.map((item,index) => {
         <FormGroup>
           <Input
             name = {item}
             value = {item}
             checked = {this.state.checked[index]}
             onChange = {()=>this.handleChange(index)}
         </FormGroup>
        })
     }
   )
 }
}

The code has been provided but this isn't the approach, you cannot expect people to do your homework for you, try something out and then point out if something is not working out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM