2

The CheckBox present in the official website only supports Android.

How can we implement checkbox in react-native which can be supported in both iOS and Android?

4 Answers 4

1

You can try using <CheckBox /> component of NativeBase and it should work on both platforms.

Example:

     <CheckBox 
        checked={this.state.checked}
        onPress={()=>this.setState({ checked: !this.state.checked})}
      />
Sign up to request clarification or add additional context in comments.

Comments

0

If you are looking for a way to render a boolean value purely within React Native, you could try a switch, like so:

<Switch value={item.selected} onValueChange={selected => this.setState({selected })/>

A switch renders like this: enter image description here

Otherwise, if you are looking for a checkbox, you could try the React Native Elements library. Like so:

import { CheckBox } from 'react-native-elements'

<CheckBox
  title='Click Here'
  checked={this.state.checked}
/>

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Thanks for the feedback, is this edit more appropriate?
0
const CheckBox = (props) => {
  const {name, value, isChecked} = props;
  return (
    <View>
      <TouchableHighlight
        onPress={() => {
          props.function(value);
        }}
        underlayColor="transparent"
        style={styles.checkBoxRowAlign}>
        {isChecked ? (
          <View>
            <View>
              <FastImage
                source={require('../assets/images/checkbox_Tick.png')}
              />
            </View>
          </View>
        ) : (
          <View>
            <View/>
          </View>
        )}
      </TouchableHighlight>
      <Text>{name}</Text>
    </View>
  );
};

export default CheckBox;

This is like a custom checkbox,if we add the above code in ur app and if u want to use it anywhere you just need to import the file and use it like

    import CheckBox from '../../CheckBox';
        <CheckBox isChecked={isChecked} name={item.name} value={item.value} function={this.onclickCheckBox} />


 onclickCheckBox(value) {
   your logic.........
  }

2 Comments

Can you add an explanation to the code sample? I think that would add to the answer you provided;
@benhorgen the first set of code is for the UI part of checkbox and in the next set first two lines are for rendering the checkbox wherever you neeeded and the below method is for the logic when u click the checkbox.
0

Step1: While forming the list to be mapped to your FlatList, map the list and add a boolean flag to maintain the state of Checkbox selection.

Step2: Set the value of Checkbox as the flag stored in your item rendered from the FlatList and onValueChange pass the index and change the flag value of the respective index of the array.

Step3: Map the array rendered in FlatList and push each item to the new array/temporary array initialised and set the temporary array to your actual array. This resolves the issue in Android and in the same render the checkbox works absolutely fine.

Step1:

const [responseList, setResponseList] = useState([])

   response.data= response.data.map(item => {
        item.isSelect = false;
        return item;
      });
      setResponseList(response.data)

Step2:

 <FlatList style={styles.flatView}
      data={responseList}
      renderItem={({ item, index }) => (
              <View style={styles.checkboxContainerList}>
                <CheckBox
                  boxType='square'
                  value={item.isSelect}
                  onValueChange={() => {
                    setData(index)
                  }}
                  tintColor={'grey'}
                  onCheckColor={colorTheme}
                  onFillColor='white'
                  onTintColor={colorTheme}
                  tintColors={{ true: colorTheme }}
                  style={styles.checkboxView}
                />

              </View>
        )}

      keyExtractor={(item, index) => index.toString()}

    />

Step3:

function setData(index) {
var isSelected = responseList[index].isSelect
responseList[index].isSelect = !isSelected

let doc = []
responseList.map(item => {
  doc.push(item)
})
setResponseList(doc)
  }

For more detailed description and complete code https://github.com/ritzblogs/reactnative-FlatListCheckbox

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.