DEV Community

Cover image for Create your own multi-selected input in React-Native
Saad
Saad

Posted on

3

Create your own multi-selected input in React-Native

We can create our multi-selected input in react-native very easily. Let’s say in our demo user can select his favorite brands from a list of options. The user can check/uncheck the inputs.

First, we need to show all the inputs to the user. We can use FlatList to render the items. Like so –

const BRANDS = [
{
name: 'Toyota',
slug: 'toyota',
},
{
name: 'Mazda',
slug: 'mazda',
},
{
name: 'Honda',
slug: 'honda',
},
{
name: 'Tesla',
slug: 'tesla',
},
.....]
export default function App() {
const [brands, setBrands] = React.useState(BRANDS);
const [selectedBrands, setSelectedBrands] = React.useState([]);
const renderBrands = ({ item, index }) => {
const { name, slug } = item;
return (
<TouchableOpacity
onPress={() => {}}
style={[styles.item]}>
<Text>{name}</Text>
</TouchableOpacity>
);
};
return (
<View style={styles.container}>
<FlatList
data={brands}
renderItem={renderBrands}
numColumns={4}
scrollEnabled={false}
/>
</View>
);
}

Then we need to keep track of the items that the user is selecting. We can keep track of them using an array. We have two conditions – When an item is already selected and when the item is not selected. So every time the user clicks on an item we need to check if it has been already selected, if it is already selected then we need to remove the item from the selected items, if not then we need to add the item to the selected items list.

We can also change the color of the button and text according to the condition. This is how our renderBrands function looks like after the proper change.

const renderBrands = ({ item, index }) => {
const { name, slug } = item;
const isSelected = selectedBrands.filter((i) => i === slug).length > 0; // checking if the item is already selected
return (
<TouchableOpacity
onPress={() => {
if (isSelected) {
setSelectedBrands((prev) => prev.filter((i) => i !== slug));
} else {
setSelectedBrands(prev => [...prev, slug])
}
}}
style={[styles.item, isSelected &amp;&amp; { backgroundColor: 'black'}]}>
<Text style={{ color: isSelected ? "white" : "black"}}>{name}</Text>
</TouchableOpacity>
);
};
view raw renderBrands.js hosted with ❤ by GitHub

The complete code can be seen in the following snack. https://snack.expo.io/@saad-bashar/68cb24

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay