Bug? Passing array to fetcher with useSWR and useSWR not updating data #1336
-
I am using a useUser hook (adapted from https://github.com/vercel/next.js/blob/canary/examples/with-iron-session/lib/useUser.js) and it never redirects and I think its because useSWR isn't updating This is my useUser hook:
Thanks in advance for any help Update: if js-cookie function Cookies.get('jwt) returns undefined then the fetcher doesn't run at all. This is the same for false which is also tried using. It only seems to run the fetcher if the arguments are strings. I have tried installing the beta version of swr but to no change. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
A couple of issues I can see. First you've identified the documentation on https://swr.vercel.app/docs/arguments#multiple-arguments so you've correctly identified that you should be doing something like Second your fetcher is never returning the promise and you probably shouldn't be catching the error (so you can use For example this is similar to what I do (you could get rid of the brackes and const fetchWithToken = (url, token) => {
return axios
.get(url, {
headers: {
Authorization: 'Bearer ' + token,
},
})
.then((res) => res.data)
} You might also want to investigate using global configuration to set the fetcher and creating an axios client with interceptors to add the authorization header and I use it to change errors for display. |
Beta Was this translation helpful? Give feedback.
A couple of issues I can see.
First you've identified the documentation on https://swr.vercel.app/docs/arguments#multiple-arguments so you've correctly identified that you should be doing something like
useSWR(["/user", Cookies.get("jwt")], fetchWithToken)
the signature for fetchWithToken would beconst fetchWithToken = (url, token) {}
when you mention getting "/" and "u" you're doing the equivalent ofurl[0]
andurl[1]
Second your fetcher is never returning the promise and you probably shouldn't be catching the error (so you can use
const { data, error } = useSWR(...)
).For example this is similar to what I do (you could get rid of the brackes and
const fetchWithToken = (url, token) => a…