Hello Friends 👋,
Welcome to Infinitbility.
As a developer, we have many times needed to check values on having array values or not.
Today, we will see how to check array contains a value or not in react native.
To check the value in an array Javascript provides the includes()
method, where we can easily able to know array have the same value or not.
Talk is cheap, so me code.
1import React, { useState } from 'react';2import { Text, View, StyleSheet, TextInput } from 'react-native';34const users = ['infintbility', 'notebility'];56export default function App() {7 const [username, setUserName] = useState('');8 return (9 <View style={styles.container}>10 <TextInput placeholder="username" style={{borderBottomWidth: 1, padding: 10}} onChange={(ev) => setUserName(ev.target.value)} />11 <Text style={styles.paragraph}>{users.includes(username) ? 'Username not available' : 'Available'}</Text>12 </View>13 );14}1516const styles = StyleSheet.create({17 container: {18 flex: 1,19 justifyContent: 'center',20 backgroundColor: '#ecf0f1',21 padding: 8,22 },23 paragraph: {24 margin: 24,25 fontSize: 18,26 fontWeight: 'bold',27 textAlign: 'center',28 },29});
Output

In the above, exmple I have created the example “When username value already exists in our array then show Username not available”
Thanks for Reading…
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.