1

How to use checkbox in loop with diffrent key when i use in loop and click on any one after than check all loop checkbox give me solution

2 Answers 2

1

You can create an array of solutions in state:

...
state.solutions = [
  {value:false},
  {value:false},
  {value:false}  
]

Create a changes event handler:

changeEvent = (ev, index) => {
  let tmp_solution = [...state.solutions];
  tmp_solutions[index].value = !tmp_solutions[index].value;
  this.setState({solutions: tmp_solution})
}

Create the checkboxes render function:

const checkBoxes = this.state.solutions.map((index) =>{
  return(
    <CheckBox 
      value={this.state.solutions[index].value} 
      onValueChange={(ev) => this.changeEvent(ev, index)} key={index} 
    />
  )
});

Render all the checkboxes:

render() {
  <View>
    {checkBoxes}
  </View>
}
...

sorry if there are errors

Sign up to request clarification or add additional context in comments.

Comments

0

Well, suppose that you have a data, so you can use map to do that. In order to save the answers also you can use index in setSetate as below. I use the react-native-checkbox-heaven component to have the check Box:

import CheckBox from 'react-native-checkbox-heaven';
renderContentCheckBox=()=>{
         console.log("[renderContent] your data", this.state.data);
            return Object.keys(this.state.data).map(function (item, index) {

              return (<View key={index} style={{  alignItems: 'flex-start', justifyContent: 'flex-start', width: Dimensions.get('window').width / 1.1, }}>
                       <CheckBox
                        label={item.title}
                        labelStyle={styles.labelStyle}
                        iconSize={28}
                        iconName='matMix'
                        checked={this.state.check1}
                        checkedColor='#44FF00'
                        uncheckedColor='#FFFFFF'
                        onChange={(val) => {(value) => { that.setState({ ["CheckBox" + index]: value,   }); console.log("radio" + index, value);
                            }}
                        />

                    </View>
                   );
            }
}

Then you can use it inside the render:

render() {

return (<View>
  {this.renderContentCheckBox(this)}
    </View>
 );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.