{"id":13037,"date":"2025-08-01T12:21:05","date_gmt":"2025-08-01T12:21:05","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13037"},"modified":"2025-08-01T12:21:32","modified_gmt":"2025-08-01T12:21:32","slug":"category-filter-with-touchableopacity","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react-native\/category-filter-with-touchableopacity","title":{"rendered":"Filter by Category Using Button or Touchableopacity"},"content":{"rendered":"<p>Here we&#8217;re providing a list of solutions to implement category filtering in React Native, ordered by best practices and common use cases.<\/p>\n<h2>Solution 1:<\/h2>\n<p>Using a functional component with useState and filter method (Recommended for modern apps):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React, { useState } from 'react';\r\nimport { View, TouchableOpacity, Text, FlatList, StyleSheet } from 'react-native';\r\n\r\nconst CategoryFilter = () => {\r\n const [selectedCategory, setSelectedCategory] = useState('all');\r\n const categories = ['all', 'electronics', 'clothing', 'books'];\r\n const items = [\r\n   { id: 1, name: 'iPhone', category: 'electronics' },\r\n   { id: 2, name: 'T-shirt', category: 'clothing' },\r\n   { id: 3, name: 'Novel', category: 'books' },\r\n ];\r\n\r\n const filteredItems = selectedCategory === 'all'\r\n   ? items\r\n   : items.filter(item => item.category === selectedCategory);\r\n return (\r\n   <View style={styles.container}>\r\n     <View style={styles.categoriesContainer}>\r\n       {categories.map(category => (\r\n         <TouchableOpacity\r\n           key={category}\r\n           style={[\r\n             styles.button,\r\n             selectedCategory === category &#038;&#038; styles.selected\r\n           ]}\r\n           onPress={() => setSelectedCategory(category)}\r\n         >\r\n           <Text>{category}<\/Text>\r\n         <\/TouchableOpacity>\r\n       ))}\r\n     <\/View>\r\n     <FlatList\r\n       data={filteredItems}\r\n       keyExtractor={item => item.id.toString()}\r\n       renderItem={({ item }) => (\r\n         <View style={styles.item}>\r\n           <Text>{item.name}<\/Text>\r\n         <\/View>\r\n       )}\r\n     \/>\r\n   <\/View>\r\n );\r\n};\r\n\r\nexport default CategoryFilter;\r\n<\/pre>\n<p>This is the recommended modern approach using functional components and hooks. It provides:<br \/>\n&#8211; Clean and maintainable code structure<br \/>\n&#8211; Efficient state management with useState<br \/>\n&#8211; Reusable components<br \/>\n&#8211; Easy to extend with additional features<\/p>\n<h2>Solution 2:<\/h2>\n<p>Using a class component with multiple filter categories:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React, { Component } from 'react';\r\nimport { View, TouchableOpacity, Text, FlatList, StyleSheet } from 'react-native';\r\n\r\nclass CategoryFilter extends Component {\r\n state = {\r\n   selectedCategories: {\r\n     electronics: false,\r\n     clothing: false,\r\n     books: false,\r\n   }\r\n };\r\n\r\n toggleCategory = (category) => {\r\n   this.setState(prevState => ({\r\n     selectedCategories: {\r\n       ...prevState.selectedCategories,\r\n       [category]: !prevState.selectedCategories[category]\r\n     }\r\n   }));\r\n };\r\n\r\n render() {\r\n   const { selectedCategories } = this.state;\r\n   const items = [\r\n     { id: 1, name: 'iPhone', category: 'electronics' },\r\n     { id: 2, name: 'T-shirt', category: 'clothing' },\r\n     { id: 3, name: 'Novel', category: 'books' },\r\n   ];\r\n\r\n   const filteredItems = items.filter(item =>\r\n     selectedCategories[item.category]\r\n   );\r\n\r\n   return (\r\n     <View style={styles.container}>\r\n       <View style={styles.categoriesContainer}>\r\n         {Object.keys(selectedCategories).map(category => (\r\n           <TouchableOpacity\r\n             key={category}\r\n             style={[\r\n               styles.button,\r\n               selectedCategories[category] &#038;&#038; styles.selected\r\n             ]}\r\n             onPress={() => this.toggleCategory(category)}\r\n           >\r\n             <Text>{category}<\/Text>\r\n           <\/TouchableOpacity>\r\n         ))}\r\n       <\/View>\r\n       <FlatList\r\n         data={filteredItems}\r\n         keyExtractor={item => item.id.toString()}\r\n         renderItem={({ item }) => (\r\n           <View style={styles.item}>\r\n             <Text>{item.name}<\/Text>\r\n           <\/View>\r\n         )}\r\n       \/>\r\n     <\/View>\r\n   );\r\n }\r\n}\r\nexport default CategoryFilter;\r\n<\/pre>\n<p>This approach uses class components and is useful when:<br \/>\n&#8211; Working with legacy code<br \/>\n&#8211; Need to maintain multiple filter states<br \/>\n&#8211; Require more complex state management<br \/>\n&#8211; Need to handle multiple selected categories<\/p>\n<h2>Solution 3:<\/h2>\n<p>Using a custom hook for reusable filtering logic:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport { useState } from 'react';\r\n\r\nconst useCategoryFilter = (items, categories) => {\r\n \/\/ Initialize selected category as 'all'\r\n const [selectedCategory, setSelectedCategory] = useState('all');\r\n\r\n \/\/ Filter items based on selected category\r\n const filteredItems = selectedCategory === 'all'\r\n   ? items \/\/ Return all items if 'all' is selected\r\n   : items.filter(item => item.category === selectedCategory);\r\n\r\n return {\r\n   selectedCategory,\r\n   setSelectedCategory,\r\n   filteredItems\r\n };\r\n};\r\n\r\nexport default useCategoryFilter;\r\n<\/pre>\n<p>This solution demonstrates:<br \/>\n&#8211; Custom hook for reusable filtering logic<br \/>\n&#8211; Separation of concerns<br \/>\n&#8211; Easy to test and maintain<br \/>\n&#8211; Can be shared across components<\/p>\n<h2>Conclusion:<\/h2>\n<p>When implementing category filtering in React Native, consider these key points:<\/p>\n<ul>\n<li>Use functional components with hooks for modern React Native apps<\/li>\n<li>Consider using custom hooks for reusable filtering logic<\/li>\n<li>Implement proper state management for selected categories<\/li>\n<li>Use FlatList for efficient list rendering<\/li>\n<li>Style your filter buttons for a good user experience<\/li>\n<li>Handle edge cases like &#8216;all&#8217; category or no items<\/li>\n<\/ul>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/react-native-offline-support\" target=\"_blank\">React Native Offline Support<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here we&#8217;re providing a list of solutions to implement category filtering in React Native, ordered by best practices and common use cases. Solution 1: Using a functional component with useState and filter method (Recommended for modern apps): import React, { useState } from &#8216;react&#8217;; import { View, TouchableOpacity, Text, FlatList, StyleSheet } from &#8216;react-native&#8217;; const [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13038,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-13037","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-native"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13037"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13037"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13037\/revisions"}],"predecessor-version":[{"id":13039,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13037\/revisions\/13039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13038"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}