I have a list of items and I'm using useInfiniteQuery for pagination stuff,
So I have a case where I can edit Item info then re-fetch the list again to get the updated list of items,
So I'm trying to re-fetch the list using
refetchPage() FC it works when I'm on the first page 1
but
current behavior:
when I scroll down to page 2 "last page at this moment." then update the item when
using queryClient.refetchQueries
it replaces the prev data with the re-fetched data on the current page.
await queryClient.refetchQueries(['getUsualOrders'], {
stale: true,
});
using refetch().
refetchUsualOrderList({
refetchPage: (page, index) => {
return index === 0;
},
});
it replaces the prev data with current page data without the updated data
Actually, it sends a request for the next page "that doesn't exist"
So how can i re-fetch the data and keep the previous data?
Code
Hook
export const useUsualOrders = ({lang, pageNumber = 1, token}) => {
return useInfiniteQuery(
['getUsualOrders'],
() => getUsualOrders({lang, pageNumber, token}),
{
getNextPageParam: lastPage => {
if (lastPage.next !== null) {
return lastPage.next;
}
return undefined;
},
},
);
};
UI
const [currentPageNumber, setCurrentPageNumber] = useState(1);
const {
data: usualOrdersList,
hasNextPage,
fetchNextPage,
isFetchingNextPage,
refetch: refetchUsualOrderList,
} = useUsualOrders({
lang,
token: currentUserInfo?.token,
pageNumber: currentPageNumber,
});
const loadMore = () => {
if (hasNextPage) {
setCurrentPageNumber(prev => prev + 1);
fetchNextPage();
}
};
const onRefetch = ()=>{
refetchUsualOrderList({
refetchPage: (page, index) => {
return index === 0;
},
});
}
<FlatList
showsVerticalScrollIndicator={false}
data={usualOrdersList.pages.map(page => page.results).flat()}
renderItem={renderModelItems}
keyExtractor={item => item.id}
onEndReached={loadMore}
onEndReachedThreshold={0.3}
// refreshControl={
// <RefreshControl refreshing={refreshing} onRefresh={onRefresh} />
// }
style={{
backgroundColor: secondary_bg_color,
}}
contentContainerStyle={[
styles.modelList,
{
backgroundColor: secondary_bg_color,
paddingHorizontal: PADDING_HORIZONTAL.SMALL,
},
]}
ListFooterComponent={
isFetchingNextPage ? (
<ActivityIndicator color={buttonBgColor} />
) : null
}
/>
response body
I created an auto scroll flatlist for the home screen. When the user logout, it would direct the user to the login screen. The problem is that I got TypeError: null is not an object (evaluating 'ref.current.scrollToIndex') when the app direct the user to the login screen. What happen and how do I resolve it?
Auto scroll flatlist component:
export const BannerCarousel = React.forwardRef((props, ref) => {
let index = 0;
const totalIndex = props.data.length ;
useEffect (() => {
setInterval (() => {
index = index + 1;
if(index < totalIndex) {
ref.current.scrollToIndex({animated: true, index: index})
} else {
ref.current.scrollToIndex({animated: true, index: 0})
index = 0;
}
}, 3000)
}, []);
return (
<View style={{paddingHorizontal: 10}} >
<FlatList
ref={ref}
data={props.data}
keyExtractor={data => data.id}
renderItem={renderItem}
/>
</View>
);
});
Home.js
const ref = React.createRef()
return (
<BannerCarousel fromLocal={true} data={TopBannerData} ref={ref}/>
);
I found that the error occurs because the BannerCarousal component is trying to find the ref.current. The ref.current is gone when the user is redirected to the Login screen. That's why the error null is not an object occurs. This is the solution that I did:
export const BannerCarousel = React.forwardRef((props, ref) => {
let index = 0;
const totalIndex = props.data.length ;
useEffect (() => {
setInterval (() => {
if(ref.current !== null) {
index = index + 1;
if(index < totalIndex) {
ref.current.scrollToIndex({animated: true, index: index})
} else {
ref.current.scrollToIndex({animated: true, index: 0})
index = 0;
}
}
}, 3000)
}, []);
return (
<View style={{paddingHorizontal: 10}} >
<FlatList
ref={ref}
data={props.data}
keyExtractor={data => data.id}
renderItem={renderItem}
/>
</View>
);
});
Where you are referencing the useRef flatListRef ? it seems like its not referencing any Valid Node. So it returns..
TypeError: null is not an object (evaluating 'flatListRef.current.scrollToIndex')
Hey initially ref might not be attached in first render, hence you could have done it like this #Nomel
setInterval (() => {
index = index + 1;
if(index < totalIndex) {
ref?.current?.scrollToIndex({animated: true, index: index})
} else {
ref?.current?.scrollToIndex({animated: true, index: 0})
index = 0;
}
}, 3000)
Anyways your check of not null works fine too :)
I'm sorry because this is going to be a long post, but I would really appreciate help. I have been trying to make it work, but with no luck.
I am making a call to the database to grab images, during this call I want the loader to be active. The loader is not showing up during the call (when I hard code it it works). An other problem is the <NoResults /> component. It is supposed to render when a query to the database comes back empty. However, this component is rendering while the api call is running.
TLDR I want a loader during the api call, but instead the <NoResults /> component is rendering and then the data that returned from the db is rendering.
Using mongodb, express, mobx, and react.
Media Store Mobx:
export class MediaStore {
#observable loading = true
#observable trending = []
#action setLoading = (bool) => { this.loading = bool }
#action getTrending = async (category, pageNum, input) => {
this.setLoading(true)
this.error = false
let cancel
axios({
method: 'GET',
url: `http://localhost:3001/media/trending?category=${category}&page=${pageNum}&input=${input}`,
cancelToken: new axios.CancelToken(c => cancel = c)
}).then(res => {
this.trending =
[...new Set([...this.trending, ...res.data.creators]
.map(JSON.stringify))].map(JSON.parse)
this.setHasMore(res.data.creators.length > 0)
this.setLoading(false)
}).catch(e => {
if (axios.isCancel(e)) return
this.error = true
})
return () => cancel
}
}
MediaCards Component:
const MediaCards = inject('userStore', 'mediaStore')(observer((props) => {
const ref = useCreators(props.mediaStore);
const location = useLocation()
const classes = useStyles()
const { isLoggedIn, favorites } = props.userStore;
const { trending, loading } = props.mediaStore;
const { media, header, mediaCard } =
location.pathname === '/dashboard' && (!isLoggedIn || !favorites.length)
? { media: [], header: 'basic', mediaCard: false }
: location.pathname === '/dashboard'
? { media: favorites, header: 'basic', mediaCard: true }
: { media: trending, header: 'explore', mediaCard: true }
const renderMediaCards = (media) => {
return media.map((data, i) => {
let isFavorite = favorites.some(f => data._id === f._id)
if (header === 'explore' && media.length === i + 1) {
return <MediaCard lastRef={ref} id={data._id} img={data.img} isFavorite={isFavorite} twitchName={data.twitch} key={data._id} />
}
return <MediaCard id={data._id} img={data.img} isFavorite={isFavorite} twitchName={data.twitch} key={Math.random()} />
})
}
return (
<>
<Header page={header} />
{header === 'explore' ? <CategoryBar /> : <Paper className={classes.paperTopMedia}></Paper>}
{mediaCard
? <Paper className={classes.paperMedia}>
<Grid container>
<GridList cellHeight={180} className={classes.rootMedia}>
{!trending.length && !loading && <NoResults />}
{!loading && renderMediaCards(media)}
{loading && <Loading />}
</GridList>
</Grid>
</Paper>
: <EmptyCard />
}
</>
)
}))
I'm getting my data from an SQLite database and showing the data when the tab is focused. The problem is that the Scrollview displaying the data is being duplicated whenever I move to another tab. How do I prevent this from happening?
componentDidMount() {
this._subscribe = this.props.navigation.addListener('didFocus', () => {
this.retrieveSavedCards();
});
}
This is how I'm retrieving the data
retrieveSavedCards(){
db.transaction(
tx => {
tx.executeSql('select * from MyScannedCards', [], (trans, result) => {
var len = result.rows.length;
for (i=0;i<len;i++){
cardDetails = result.rows._array[i];
this.setState({ cardholders: this.state.cardholders.concat(cardDetails) })
}
});
}
);
}
This is my scrollview and how the data is supposed to be displayed:
<ScrollView>
{
this.state.cardholders.map((l, i) => (
<ListItem
key={i}
leftAvatar={{ source: { uri: this.state.baseUrl+l.avatar } }}
title={l.cardholderfirstname + " " + l.cardholderlastname}
subtitle="Your company"
onPress={() => {
this.toggleModal();
}}
/>
))
}
</ScrollView>
This is my state:
this.state = {
cardholders: [
],
baseUrl: "http://myurl.com"
};
You will have to unmount the didFocus event listener.
As mentioned here : https://reactnavigation.org/docs/en/status-bar.html#tabnavigator
componentWillUnmount() {
this._subscribe.remove();
}
Bit of a hack, but I couldn't figure out how to stop the scrollview from duplicating so instead I made the scrollview stop displaying an object already in state.
In my for loop:
for (i=0;i<len;i++){
cardDetails = result.rows._array[i];
console.log(cardDetails.id)
this.addUser(cardDetails);
}
And then additional functionality to check for duplicates and update state if something new is found.
userExists(cardDetails) {
return this.state.cardholders.some(function(el) {
return el.id === cardDetails.id;
});
}
addUser(cardDetails) {
if (this.userExists(cardDetails)) {
console.log("Skipping")
return false;
}
this.setState({ cardholders: this.state.cardholders.concat(cardDetails) })
console.log("Adding")
return true;
}
I am trying to search through a flatlist based on a search bar text. The problem I am running into is that when the user mistypes...say they wanted to type "burger" but typed "burget" by mistake then it returns nothing as it should. When the user deletes the "t" then it should re-render the flatlist again with the last text matching the "burge" part.
note: using react-native-elements search bar which allows me to call the text with just e or event.
What I have so far in the Main.js file:
searchText = (e) => {
let text = e.toLowerCase();
let trucks = this.state.data;
// search by food truck name
let filteredName = trucks.filter((truck) => {
return truck.name.toLowerCase().match(text);
});
// if no match and text is empty
if(!text || text === '') {
console.log('change state');
this.setState({
data: initial
});
}
// if no name matches to text output
else if(!Array.isArray(filteredName) && !filteredName.length) {
console.log("not name");
this.setState({
data: [],
});
}
// if name matches then display
else if(Array.isArray(filteredName)) {
console.log('Name');
this.setState({
data: filteredName,
});
}
};
<View style={styles.container}>
<SearchBar
round
lightTheme
containerStyle={styles.search}
ref="search"
textInputRef="searchText"
onChangeText={this.searchText.bind(this)}
placeholder='Search by Truck Name...'
/>
<TruckList getTruck={(truck) => this.setTruck(truck)} truckScreen={this.truckScreen} data={this.state.data}/>
</View>
then the TruckList.JS:
export default class TruckList extends Component {
// rendering truck screen
renderTruckScreen = (item) => {
this.props.truckScreen();
this.props.getTruck(item);
}
render() {
return(
<List style={styles.list}>
<FlatList
data={this.props.data}
renderItem={({ item }) => (
<ListItem
roundAvatar
avatar={{uri: item.pic1}}
avatarStyle={styles.avatar}
title={item.name}
titleStyle={styles.title}
subtitle={
<View style={styles.subtitleView}>
<Text style={styles.subtitleFood}>{item.food}</Text>
<View style={styles.subtitleInfo}>
<Icon
name="favorite"
size={20}
color={"#f44336"}
style={styles.subtitleFavorite}
/>
<Text style={styles.subtitleFavoriteText}>{item.favorited} favorited</Text>
</View>
</View>
}
onPress={() => this.renderTruckScreen(item)}
/>
)}
keyExtractor={(item) => item.uid}
ListFooterComponent={this.footer}
/>
</List>
)
}
}
I have tried a few other ways to no avail. Also the only solutions I have seen working for React Native are with ListView which will be depreciated in time. So I am trying to do this with the new FlatList Component.
Thanks for your help!
I came across this same issue today when trying to implement a filter / search function on the new FlatList component. This is how I managed to solve it:
By creating another item in the state of the parent component called noData, you can set that to true when there are no results that match your search and then render your FlatList conditionally.
My implementation is slightly different to yours, but if I had to adjust your code it would look something like this:
Searchtext function:
searchText = (e) => {
let text = e.toLowerCase()
let trucks = this.state.data
let filteredName = trucks.filter((item) => {
return item.name.toLowerCase().match(text)
})
if (!text || text === '') {
this.setState({
data: initial
})
} else if (!Array.isArray(filteredName) && !filteredName.length) {
// set no data flag to true so as to render flatlist conditionally
this.setState({
noData: true
})
} else if (Array.isArray(filteredName)) {
this.setState({
noData: false,
data: filteredName
})
}
}
Then pass the noData bool to your TruckList component:
<TruckList getTruck={(truck) => this.setTruck(truck)}
truckScreen={this.truckScreen} data={this.state.data} noData={this.state.noData}/>
Then render your FlatList in the TruckList component only if there are results:
<List style={styles.list}>
{this.props.noData ? <Text>NoData</Text> : <FlatList {...} />}
</List>
That should then take care of handling user typing errors - as it will re-render the flatlist as soon as there are no results, and will remember the previous search state when you remove the typing error..
Let me know if that helps!
For a useful in-memory search you should keep initial data seperately.
I have more simple solution for this.
This solution for in-memory search on FlatList's data and uses it String.prototype.includes() method to search substring.
You can find full source code of this component in this gist;
https://gist.github.com/metehansenol/46d065b132dd8916159910d5e9586058
My initial state;
this.state = {
searchText: "",
data: [],
filteredData: []
};
My SearchBar component (it comes from react-native-elements package);
<SearchBar
round={true}
lightTheme={true}
placeholder="Search..."
autoCapitalize='none'
autoCorrect={false}
onChangeText={this.search}
value={this.state.searchText}
/>
My search method;
search = (searchText) => {
this.setState({searchText: searchText});
let filteredData = this.state.data.filter(function (item) {
return item.description.includes(searchText);
});
this.setState({filteredData: filteredData});
};
And last my FlatList's DataSource expression;
<FlatList
data={this.state.filteredData && this.state.filteredData.length > 0 ? this.state.filteredData : this.state.data}
keyExtractor={(item) => `item-${item.id}`}
renderItem={({item}) => <ListItem
id={item.id}
code={item.code}
description={item.description}
/>}
/>
Happy coding...
Update:
This blog can help you better understand the searching in a FlatList.
FYI:
If you have huge online data then you can also use algolia.
I adjusted the above code for me in order to make it work properly. The reason is that when user removes the last wrong character, code search this new string from a previous search list (state) which does not contain all objects, although it had to search from a full list available. So, I have two list now. One contains full list of objects and second contains only rendered list of objects which is changing upon search.
handleSearchInput(e){
let text = e.toLowerCase()
let fullList = this.state.fullListData;
let filteredList = fullList.filter((item) => { // search from a full list, and not from a previous search results list
if(item.guest.fullname.toLowerCase().match(text))
return item;
})
if (!text || text === '') {
this.setState({
renderedListData: fullList,
noData:false,
})
} else if (!filteredList.length) {
// set no data flag to true so as to render flatlist conditionally
this.setState({
noData: true
})
}
else if (Array.isArray(filteredList)) {
this.setState({
noData: false,
renderedListData: filteredList
})
}
}
Make Search Bar Filter for List View Data in React Native
For Real-Time Searching in List View using Search Bar Filter
We will load the list from the network call and then show it to the user.
The user can search the data by entering the text in TextInput.
After inserting the text SearchFilterFunction will be called We will
compare the list data with the inserted data and will make a new Data
source.
We will update the data source attached to the ListView.
It will re-render the list and the user will be able to see the
filtered data.
//This is an example code to Add Search Bar Filter on Listview//
import React, { Component } from 'react';
//import react in our code.
import {
Text,
StyleSheet,
View,
FlatList,
TextInput,
ActivityIndicator,
Alert,
} from 'react-native';
//import all the components we are going to use.
export default class App extends Component {
constructor(props) {
super(props);
//setting default state
this.state = { isLoading: true, text: '' };
this.arrayholder = [];
}
componentDidMount() {
return fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(responseJson => {
this.setState(
{
isLoading: false,
dataSource: responseJson
},
function() {
this.arrayholder = responseJson;
}
);
})
.catch(error => {
console.error(error);
});
}
SearchFilterFunction(text) {
//passing the inserted text in textinput
const newData = this.arrayholder.filter(function(item) {
//applying filter for the inserted text in search bar
const itemData = item.title ? item.title.toUpperCase() : ''.toUpperCase();
const textData = text.toUpperCase();
return itemData.indexOf(textData) > -1;
});
this.setState({
//setting the filtered newData on datasource
//After setting the data it will automatically re-render the view
dataSource: newData,
text: text,
});
}
ListViewItemSeparator = () => {
//Item sparator view
return (
<View
style={{
height: 0.3,
width: '90%',
backgroundColor: '#080808',
}}
/>
);
};
render() {
if (this.state.isLoading) {
//Loading View while data is loading
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<ActivityIndicator />
</View>
);
}
return (
//ListView to show with textinput used as search bar
<View style={styles.viewStyle}>
<TextInput
style={styles.textInputStyle}
onChangeText={text => this.SearchFilterFunction(text)}
value={this.state.text}
underlineColorAndroid="transparent"
placeholder="Search Here"
/>
<FlatList
data={this.state.dataSource}
ItemSeparatorComponent={this.ListViewItemSeparator}
renderItem={({ item }) => (
<Text style={styles.textStyle}>{item.title}</Text>
)}
enableEmptySections={true}
style={{ marginTop: 10 }}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}
const styles = StyleSheet.create({
viewStyle: {
justifyContent: 'center',
flex: 1,
marginTop: 40,
padding: 16,
},
textStyle: {
padding: 10,
},
textInputStyle: {
height: 40,
borderWidth: 1,
paddingLeft: 10,
borderColor: '#009688',
backgroundColor: '#FFFFFF',
},
});
Click Hear for more idea
Here is my solution:
You need to have a backup of your data
this.state = {
data: [],
backup: []
}
on search method
search = txt => {
let text = txt.toLowerCase()
let tracks = this.state.backup
let filterTracks = tracks.filter(item => {
if(item.name.toLowerCase().match(text)) {
return item
}
})
this.setState({ data: filterTracks })
}
Explanation: when calling setState on your data it will changed to current state and cannot be changed again.
So backup data will handle to filter your data.
ref - https://medium.freecodecamp.org/how-to-build-a-react-native-flatlist-with-realtime-searching-ability-81ad100f6699
constructor(props) {
super(props);
this.state = {
data: [],
value: ""
};
this.arrayholder = [];
}
Next fetching data :-
_fetchdata = async () => {
const response = await fetch("https://randomuser.me/api?results=10");
const json = await response.json();
this.setState({ data: json.results });
this.arrayholder = json.results;
};
Next define searchFilterFunction :-
searchFilterFunction = text => {
this.setState({
value: text
});
const newData = this.arrayholder.filter(item => {
const itemData = item.email.toLowerCase();
const textData = text.toLowerCase();
return itemData.indexOf(textData) > -1;
});
this.setState({ data: newData });
};
rendering searchView:-
<TextInput
style={{ height: 40, borderColor: "gray", borderWidth: 1 }}
onChangeText={text => this.searchFilterFunction(text)}
/>
Don't forget to import TextInput from "react-native";
You can Search your data by following these steps :
<TextInput onChangeText={(text) => searchData(text)} value={input} />
***Please Note *searchData is my function whom I passing a text prop***
const searchData = (text) => {
const newData = restaurantsData.filter((item) => {
return item.title.search(text) > -1;
});
setRestaurantsData(newData);
setInput(text);
};
Note RestaurantsData is my data array
FYI : data is the subtext to be searched, this is a basic search implemented as the data to be searched is looked into every list item of an array which is a copy of the actual array/array of objects and finally its state is set whether match found or not between 0 to (actualArray.length-1) and the temporary arrayData is rendered if there is at least one match else actualArray is rendered
implementSearch(data) {
temp = [];
var count = 0;
var searchData = data.toUpperCase();
var arr = this.state.personDetail;
for (var i = 0; i < arr.length; i++) {
var actualData = arr[i].name.toUpperCase();
if (actualData.includes(searchData)) {
temp.push(arr[i]);
count++;
}
}
this.setState({
tempArray: temp,
matches: count,
searchValue: data
});
}
Hope this helps
My search method; from #metehan-senol
search = (searchText) => {
this.setState({searchText: searchText});
let filteredData = this.state.data.filter(function (item) {
return item.description.includes(searchText);
});
this.setState({filteredData: filteredData});
};
the search method of could be simplify and Eslint proof like so
search = (searchText) => {
const searched = searchText.toLowerCase();
this.setState(prevState => ({
searchText: searched,
filteredData: prevState.data.filter(item =>
item.description.toLowerCase().includes(searched)
),
}));
};
Do filter by applying
let filterData= data.filter((item) => {
return item.name.toLowerCase().match(text)
})
if (!text || text === '') {
this.setState({
datasource: initial
})
} else if (!Array.isArray(filterData) && !filterData.length) {
// set no data flag to true so as to render flatlist conditionally
this.setState({
noData: true
})
} else if (Array.isArray(filterData)) {
this.setState({
noData: false,`enter code here`
dataSource: filterData
})`enter code here`
}
This is not the best solution in terms of performance, but if you do not have a large amount of data, then feel free to use this function:
searchFilter () {
return this.props.data.filter((item) => {
const regex = new RegExp(this.state.searchInput, "gi")
return item.label.match(regex);
})
}
And then in your FlatList component:
<FlatList
data={this.searchFilter()}
renderItem={this.renderItem}
keyExtractor={(item) => item.value}
/>
const SearchUser = (e) =>{
console.log(e)
const setProject = Project.filter(item => item.name.toLowerCase().includes(e.toLowerCase()) )
console.log(setProject)
setfetch(setProject)
}