-
Disclaimer: I have 0 familiarity with Observables. So, I have been trying to use ReactFire in a new project and the first functionality I require is to have a filtered query. Basically I have a list of "Prizes" and I'd like to be able to filter the prizes based on "time". This has been traditionally easy with the firebase sdk, however I can't seem to replicate this functionality and I am unsure if this is misguided use of the library or a shortcoming of it. Following is a concrete example: function Prizes() {
const { year, weekNumber } = usePrizes() as any
const [state, setState] = useState<State<Prize>>({
status: 'loading',
data: [],
error: undefined,
})
const { status, data, error } = state
const ref = useFirestore().collection('prizes').limit(3)
useEffect(() => {
console.log('getting new data', { year, weekNumber })
ref
.where('year', '==', year)
.where('weekNumber', '==', weekNumber)
.get()
.then((snap) => {
console.log('Retrieved', snap, snap.size)
setState({
data: snap.docs.map((doc) => doc.data() as Prize),
status: 'success',
error: undefined,
})
})
.catch((e) => {
console.error('Error', e)
setState({ status: 'error', data: [], error: e })
})
}, [weekNumber, year])
if (status === 'loading') return <p>Loading</p>
if (status === 'error') return <p>{error?.message || ''}</p>
if (error !== undefined)
return (
<p>
Something went wrong:{error.name} -- {error.message}
</p>
)
return <pre>{JSON.stringify({ status, data, error })}</pre>
} With the above component I get correct results on first load however when I change the filters for this query, I simply get empty results - even when returning to the initial value. The filters are I've also tried to leverage |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi, It seems like most of the code your writing is already done in ReactFire's The // imports, as necessary
const PrizeList = () => {
const { year, weekNumber } = usePrizes() as any;
const firestore = useFirestore();
const prizeListRef = firestore
.collection('prizes')
.limit(3)
.where('year', '==', year)
.where('weekNumber', '==', weekNumber);
const { status, data, error } = useFirestoreCollectionData(prizeListRef);
if (status === 'loading') return <p>Loading</p>
if (status === 'error') return <p>{error?.message || ''}</p>
if (error !== undefined)
return (
<p>
Something went wrong:{error.name} -- {error.message}
</p>
)
return <pre>{JSON.stringify({ status, data, error })}</pre>
To use ReactFire, very little knowledge is required about how the observable works. I hope that answers your question! |
Beta Was this translation helpful? Give feedback.
Hi,
It seems like most of the code your writing is already done in ReactFire's
useFirestoreCollectionData
. I highly recommend using that unless you need some advanced customization or filtering of the data received (such as creating your own OR query). Unless you are using other features of reactfire besides useFirestore, you might as well just use the vanilla JS SDK, because that hook just returns Firestore (with maybe some suspense depending on your configuration).The
useFirestoreCollectionData
acts as a react hook, which you would typically call inside a react component. As an argument for theuseFirestoreCollectionData
, you should supply the Firestore query. Whenever the argument for…