dimanche 1 mars 2020

props.onClick undefined checkbox React Native

I am using the Checkbox component for my code and I have a list of elements and for each element I have a checkbox. However, my checkbox returns the following error when I check it on the phone simulator :

TypeError: this.props.onClick is not a function. (In 'this.props.onClick()', 'this.props.onClick' is undefined)

I don't understand because a checkbox does not require the onClick function ...

Please see my code below if needed:

import CheckBox from 'react-native-check-box'

export default class List extends Component {
constructor(props) {
   super(props)

   this.state = {
      names: [
         {
            key: 0,
            name: 'Element1',
            isChecked: false
         },
         {
            key: 1,
            name: 'Element2',
            isChecked: false, 
         }
      ]
   }
}

   checkboxTest(key){
      let names = this.state.names.reduce((acc,name)=>{
         if(name.key === key){
            return {...name, isChecked: !name.isChecked}
         }
         return name
      }, [])
      this.setState({names})
      console.table(names)
   }


   render(){
      return (
        <ScrollView>

         <View>
            {
               this.state.names.map((item) => (
                <View style={styles.background}>  
                <Text style = {styles.title}>{item.name}</Text>
                <CheckBox value={false} onChange={()=>this.checkboxTest(item.key)}/>
                </View>
               ))
            }
         </View>
         </ScrollView> 
      ) 
}
}



Aucun commentaire:

Enregistrer un commentaire