I am working on reactjs web app where i have a search bar in my home page where user can search within the website and then i am getting that search input and finding data from the firestore and i am going to the new page with the data i got from firestore and then showing the data everytning is working fine but as soon as i clikced again on the search button the data becomes null why ??
my code is
const searchFirestore = async () => {
try {
let query = dataref.collectionGroup("room_details");
if (lastVisible) {
query = query.startAfter(lastVisible);
}
// Add a filter based on the search input
if (searchInput.trim() !== "") {
query = query.where("address", "array-contains", searchInput.trim());
const snapshot = await query.limit(5).get();
const newData = snapshot.docs.map((doc) => doc.data());
navigation(`/rooms_in` , {state : {newData : newData}})
setFirebaseData(newData); // Update the state with search results
if (snapshot.docs.length > 0) {
setLastVisible(snapshot.docs[snapshot.docs.length - 1]);
} else {
setLastVisible(null);
}
}else{
toast.success("search field empty!!")
}
} catch (error) {
console.error("Error searching Firestore:", error);
}
};
I have called the search firestore method on the button Search.
the code where i am coming with the data
const [firebaseData, setFirebaseData] = useState([])
useEffect(() => {
setFirebaseData(location.state.newData);
}, [location])
and then i am setting the data in the component but when i clicked the button search again it does not shows the data. Why this is happening???