I am trying to search data and send that data to a flat list. The current code works without error however it only finds results with the an exact text searched. I am trying to filter data to give me results as long as the text that I am searching is found anywhere in the string. I need help with my handleSearch function
Example. When I search: "Good book". it finds results. If I search: "is a good book" I do not get any results
I would like to receive results for my string as long as the text is founded somewhere in the string
Example What I am hoping for. Find results if string contains: This is a Good book, or Good is a book.
// _.filter is a function I am using from Lodash
// action list is the data coming from another file: ActionListService.getList()
const ActionList = [
{
title: 'Good Book',
icon: 'good book',
action: 'good book',
},
{
title: 'Hello World',
icon: 'hello',
action: 'hello action',
},
]
const [isLoading, setIsLoading] = useState(false);
const [query, setQuery] = useState('');
const [fullData, setFullData] = useState([]);
const [data, setData] = useState([]);
useEffect(() => {
setIsLoading(true);
ActionListService.getList()
.then(results => {
setData(results);
setFullData(results);
setIsLoading(false);
})
.catch(err => {
setIsLoading(false);
});
}, []);
const handleSearch = text => {
const formattedQuery = text.toLowerCase();
const filteredData = _.filter(fullData, value => {
return value.title.toLowerCase().includes(formattedQuery);
});
setData(filteredData);
setQuery(text);
};
return (
<>
<Searchbar
placeholder="Search"
onChangeText={queryText => handleSearch(queryText)}
value={query}
/>
<View style={{paddingLeft: insets.left, paddingRight: insets.right}}>
<FlatList
data={data}
numColumns={orientation.orientation ? 1 : 3}
key={orientation.orientation ? 1 : 3}
keyExtractor={item => item.title}
renderItem={({item}) => (
<List.Item
onPress={() => createAction(item.action)}
// eslint-disable-next-line react-native/no-inline-styles
style={{
width: orientation.orientation ? '100%' : '30%',
}}
title={item.title}
left={() => <List.Icon icon={item.icon} />}
/>
)}
/>
</View>
</>
);