Forked from nafeu/using-react-query-2-table-query-component.js
Last active
September 16, 2021 17:51
-
-
Save tupton/fcde59a514b28af819eb4a7421d804c4 to your computer and use it in GitHub Desktop.
useTableQuery.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function useTableQuery(queryFn, columnDefs, formatApiData, formatParamsToQueryKey) { | |
const [queryKey, setQueryKey] = useState(''); | |
const {data, isLoading} = useQuery(queryKey, () => queryFn({ordering: queryKey.sortBy})); | |
const tableData = useMemo(() => formatApiData(data), [data]); | |
const columns = useMemo(() => columnDefs, [columnDefs]); | |
const fetchData = useCallback((params) => { | |
const paramsToQK = formatParamsToQueryKey(params); | |
setQueryKey(paramsToQK); | |
}, [tableData]); | |
return {columns, tableData, fetchData} | |
} | |
const PurchaseOrderTableInstance = () => { | |
const {columns, tableData, fetchData} = useTableOuery( | |
({queryKey}) => fetchPO({ordering: queryKey.ordering}), | |
columnDefs, | |
(data) => data?.body?.results, | |
(params) => {ordering: …} | |
); | |
if (isLoading || !tableData) { | |
return <div>Loading...</div> | |
} | |
return ( | |
<Table columns={columns} tableData={td} onFetchData={fetchData}/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment