[Question] Example or guidance to modify headers with mutations?
NoelLH opened this issue · comments
Hi, thank you for this library! It's mostly working great for me so far in a Next.js project.
I'm looking to move some WIP code to v1, as it has some bug fixes we need and improvements that look really good.
My question is how best to deal with the move of mutations to hooks, given that our use case requires a custom and varying auth header (a captcha token which expires after 2 mins).
Before, we could pass in a custom Axios right before making a call. I'm struggling to work out how to do that "late" / right before the call with the new version because React hooks can't be called in a callback fn where I was invoking the request before – only at the top level of the functional component.
Do you have any ideas? If it should be simple, do you think it would be possible to add an example that includes something like this?
Hi @NoelLH
All the things not change for new version here.
no matter we use normal swr hook or mutation hook, we will pass axios as our request client. The final request call still be sent by axios.
In this case, you can still use something like interceptors which axios can provide for us to implement your requirement.
Thanks @teobler. I think I was following what I guess is an anti-pattern before, by using something exported from v0 that wasn't actually a hook at all – which allowed me to invoke it directly in a context where hooks are not allowed.
I've now replaced this with what I think is probably the expected pattern, and used an interceptor as you suggest to give Axios new captcha tokens as needed, after the SWR hooks' instantiations. Works great! :)