1

I am a new bee in react native. Forgive me if I am asking repeated question. I am asking the relative question because the questions asked before on this topic is not helpful for me. I want to create the dynamic checkbox on the basis of JSON response, I have collected the data in an array, below is the code.

sellablePartsCategory = [];

the sellablePartsCategory contains the data

sellablePartsCategory = ["Brake", "Suspension", "Engine", "Tyre"]

renderParts(){
    for(let i=0; i<sellablePartsCategory.length; i++){
      return(
        <CheckBoxComponent 
            title={sellablePartsCategory[i]}
            checked= {true} /> );
    }
}

 render(){
 <View>
    <Text> {sellablePartsCategory} </Text>
      {this.renderParts()}
 </View> }

I know that return statement is breaking the for loop and make it run only one time. It is giving me the zero index value of array and then loop gets break. I don't know how to solve issue. The CheckBoxComponent.js is

import React, { Component } from 'react';                         
import {TextInput, StyleSheet, View, Text} from 'react-native';   
import { CheckBox, ListItem, List } from 'react-native-elements';  
const CheckBoxComponent = ({title, checked}) => {
return (
    <CheckBox
        title={title}
        checkedColor='#0D4A8E'
        checked={checked}
        />
);};

export { CheckBoxComponent };

1 Answer 1

1

In your renderParts() method, you can return all the checkboxes inside a list, using only a single return. Try the code below:

renderParts(){
    let checkBoxComponentList = [];
    for(let i=0; i<sellablePartsCategory.length; i++){
        checkBoxComponentList.push(<CheckBoxComponent 
            title={sellablePartsCategory[i]}
            checked= {true} />);
    }
    return checkBoxComponentList;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Don't forget key prop for CheckBoxComponent. Also you can use sellablePartsCategory.map(callbackFunction) to optimize this code a little)
Happy to help :) also, merged your edit to answer @fazeelreact :)
@KartalErkoc How can I handle onPress event for each check box that to be checked?
@fazeelreact , well that requires another question and answer thread, but just to give you an idea, you can keep your checked value as a state in your CheckBoxComponent and toggle it between true and false when pressed. This also requires you to add onPress prop function to your CheckBox elements just like checkedColor and checked props. You may ask that as a separate question maybe. Because, I cannot change the answer above as it addresses a correct answer to your initial question.
hi @fazeelreact , can you mark the answer as accepted? That will be helpful for me as well :)

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.