I am using React Query (for the first time) to manage calls to an API. I am fetching a list of invoices, which I've done successfully so far in the code below. I now want to filter this list based on a status property that each invoice object has - it is either 'paid', 'pending' or 'draft - using a series of checkboxes.
How would I go about this? Could the filtering be somehow integrated into the initial call to the API so I am always receiving one set of data, or would I have to make multiple calls and render the data accordingly below?
import React from 'react'
import {useQuery} from 'react-query'
import InvoiceLink from './InvoiceLink'
const fetchData = async () => {
const res = await fetch('http://localhost:3004/invoices');
return res.json();
};
export default function InvoiceList() {
const {data, isLoading, isError} = useQuery('invoices', fetchData);
return (
<div>
<ul>
<li><input type="checkbox" id="paid"></input><label htmlFor="paid">Paid</label></li>
<li><input type="checkbox" id="pending"></input><label htmlFor="pending">Pending</label></li>
<li><input type="checkbox" id="draft"></input><label htmlFor="draft">Draft</label></li>
</ul>
{isError && (
<p>Error</p>
)}
{isLoading && (
<p>Loading</p>
)}
<ul>
{data && data.map(item =>
<InvoiceLink key={item.id} invoice={item} />
)}
</ul>
</div>
)
}