how do you send json with swr #2593
Unanswered
aliber4079
asked this question in
General
Replies: 2 comments
-
oops I probably want useEffect instead |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think you could perform similar to this... We have to use fetcher function in this. import useSWR from 'swr';
const fetcher = async (url, data) => {
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('Failed to fetch data');
}
return response.json();
}
const MyComponent = () => {
const { data, error } = useSWR('/api/data', fetcher);
if (error) return <div>Error: {error.message}</div>;
if (!data) return <div>Loading...</div>;
return (
<div>
{data.message}
</div>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
all the examples in https://swr.vercel.app/docs/ show hitting a url but nothing in the docs shows how to send json.
Beta Was this translation helpful? Give feedback.
All reactions