menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

React

A community of developers, designers and others who love React.js. ⚛️

Channels
Team

Edit Component useFetch

July 12, 2021 at 1:53pm

Edit Component useFetch

July 12, 2021 at 1:53pm (Edited 2 months ago by @jonkwheeler)
Hi all, i'm trying to create an Edit page component, for a basic user api. Username, email details. Using custom useFetch hooks, for GET and PUT. I've searched through many tutorials, I have a useFetch that i put together:
export default function useFetch(url, options, { immediate }) {
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [isPending, setIsPending] = useState(false);
const executeFetch = useCallback(async () => {
setIsPending(true);
setData(null);
setError(null);
try {
const res = await fetch(url, options);
const json = await res.json();
setData(json);
} catch (e) {
setError(e);
}
setIsPending(false);
}, [url, options, data, error, isPending]);
useEffect(() => {
if (immediate) {
executeFetch();
}
}, [executeFetch, immediate]);
return { data, error, isPending, executeFetch };
}
// Component use
useFetch("/some-api-url", { method: "POST" });
Has anyone done something similar with an Edit Component, I really want to move past this now. Am at a point now where i would pay someone to do it. tired_face
No messages yet