All files / src/components/hooks getPageData.js

0% Statements 0/15
100% Branches 0/0
0% Functions 0/3
0% Lines 0/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                                                       
import { useState, useEffect, useCallback } from 'react'
 
const getPageData = (apiMethod, query) => {
	const [loading, setLoading] = useState(true)
	const [error, setError] = useState(null)
	const [data, setData] = useState(null)
 
	const fetchData = useCallback(async () => {
		setLoading(true)
		setError(null)
		try {
			const result = await apiMethod(query)
			setData(result)
		} catch (err) {
			setError(err)
		} finally {
			setLoading(false)
		}
	}, [apiMethod, JSON.stringify(query)])
 
	useEffect(() => {
		fetchData()
	}, [fetchData])
 
	return { loading, error, data, refetch: fetchData }
}
 
export default getPageData