Я сделал образец в соответствии с требованием.
import React, { Component } from "react";
import { Text, View, StyleSheet, TouchableOpacity } from "react-native";
const KEYWORDS = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
export default class App extends Component {
state = {
keywordsList: [],
};
toggleKeyword = (keyword) => {
const { keywordsList } = this.state;
let list = keywordsList;
let index = -1;
if ((index = keywordsList.indexOf(keyword)) != -1) {
list.splice(index, 1);
} else {
list.push(keyword);
}
this.setState({ keywordsList: list });
};
render() {
const { keywordsList } = this.state;
const { container, selectedKeywordStyle, buttonStyle, textStyle } = styles;
return (
<View style={container}>
{KEYWORDS.map((item) => (
<TouchableOpacity
style={keywordsList.find((element) => element == item) ? selectedKeywordStyle : buttonStyle}
onPress={() => this.toggleKeyword(item)}
>
<Text style={textStyle}>{item}</Text>
</TouchableOpacity>
))}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flexDirection: "row",
justifyContent: "space-around",
flexWrap: "wrap",
paddingTop: 50,
},
textStyle: {
fontSize: 16,
padding: 8,
textAlign: "center",
},
buttonStyle: {
width: "30%",
backgroundColor: "gray",
borderRadius: 15,
margin: 5,
},
selectedKeywordStyle: {
width: "30%",
backgroundColor: "green",
borderRadius: 15,
margin: 5,
},
});
Надеюсь, это поможет вам. Не стесняйтесь сомнений.