0

I am creating a React Native App in which I am using the Checkbox in a javascript map function. Due to that when I check one checkbox it checks all the other checkboxes as well. How can manage to check the checkboxes separately and also I could check them multiple times from the map function list.

Here is the code for some references :

<Card noShadow style={styles.card}>
                    <CardItem header bordered>
                        <Text style={styles.header}>{string.color}</Text>
                    </CardItem>
                    {this.state.details.map((data, i) => {
                        return (
                            <List key={i}>
                                <CardItem>
                                    <CheckBox style={styles.checkbox}
                                        checked={this.state.checkedDefault} onPress={() => this.setState({ checkedDefault: !this.state.checkedDefault })} />
                                    <Text note style={styles.textWrap}>{data.name}</Text>
                                </CardItem>
                            </List>
                        )
                    })}
                </Card>
5
  • This is beacuse you are setting state value this.state.checkedDefault and using same for all the check-boxes. So if it is true all check-boxes will get checked. Commented May 10, 2019 at 10:35
  • One possible solution is don't control your checkbox i.e. don't do this checked={this.state.checkedDefault} instead use refs Commented May 10, 2019 at 10:39
  • You just need to handle an array of checked fields, one per checkbox instead of just one, and reflect that state in checkbox list Commented May 10, 2019 at 10:45
  • @ravibagul91 How can I use refs in Checkbox? I haven't used it before so can you please explain it with an example? Commented May 10, 2019 at 10:52
  • check this - stackoverflow.com/questions/36833192/… Commented May 10, 2019 at 10:56

1 Answer 1

0

You need to use object for that, change your render method like below

<Card noShadow style={styles.card}>
    <CardItem header bordered>
        <Text style={styles.header}>{string.color}</Text>
    </CardItem>
    {this.state.details.map((data, i) => {
        return (
            <List key={i}>
                <CardItem>
                    <CheckBox style={styles.checkbox}
                        checked={!!this.state.checkedDefault[i]}
                        onPress={
                            () => this.setState(state => {
                                const checkedDefault = {...state.checkedDefault};
                                checkedDefault[i] = !checkedDefault[i];
                                return { checkedDefault }
                            })
                        } />
                    <Text note style={styles.textWrap}>{data.name}</Text>
                </CardItem>
            </List>
        )
    })}
</Card>

don't forget to assign initial value for checkedDefault in state, like below

state = {
   //other values
   checkedDefault: {}
}

Also you can use an id property, if exists on the items in details array instead of i to assign the value in the object

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

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.