I am doing a request with useSWR in next js and the api route needs the id of the page I am on, it is in the query params at router.query.id
But how do i pass that to useSWR so I can pull in the right data from my api router function that talks to my DB.
Here is the simple request
const router = useRouter();
//PASS ROUTER.QUERY.ID to api/dataSingle
const { data: valuesData, error: valuesError } = useSWR(
`/api/dataSingle/`,
fetcher,
);
UPDATE: If I try to pass the array as per documentation the requerst body in the api function is undefined. Here is what I have tried since reading it.
const fetcher = (url) => fetch(url).then((r) => r.json());
const router = useRouter();
const { data: valuesData, error: valuesError } = useSWR(
[`/api/dataSingle/`, router.query.id],
fetcher
);
api Route
import { query as q } from "faunadb";
import { serverClient } from "../../utils/fauna-auth";
export default async (req, res) => {
console.log(req.body);
returns undefined
};
UPDATE: I have tried the following fetcher, but still don;t grasp what it is even doing so it's not working.
const fetcher = (url, id) => fetch(url, id).then((r) => r.json());