Closed ecumene closed 3 years ago
export default function usePostAPI() { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const callAPI = useCallback(async (url, bodyJSON) => { setIsLoading(true); try { const res = await fetch(url, { method: bodyJSON ? "POST" : "DELETE", headers: { "Content-Type": "application/json", Authorization: `Bearer ${localStorage.Token}`, }, body: JSON.stringify(bodyJSON), }); const json = await res.json(); setIsLoading(false); return json; } catch (err) { setError(err.message); } return null; }, []); return [{isLoading, error}, callAPI]; }
So I moved url, bodyJSON into callAPI. Tested both post and delete, they seem to be working so far. Now need to remove data/setData state
This now works without state for data. Marking as closed.