I’m trying to wrap useSWRMutation and its result in my own hook.
Unfortunately I’m doing something wrong since the data
error
and isMutating
are not getting populated.
This is my current code:
export const useWrappedSWRMutation = (params: useParams) => {
const { data, error, isMutating, trigger } = useSWRMutation<
VerifiedEmailResponse,
AxiosError<TracedError>,
string,
useParams
>('api/endpoint', postWithAxiosFunction)
const execFunction = useCallback(async () => {
const result = await trigger(params)
return result.user_email
}, [params, trigger])
const email = data?.user_email
useEffect(() => {
console.log({ data, error, isMutating })
}, [data, error, isMutating])
return [
{ data: email, isLoading: isMutating, error: error?.response?.data },
execFunction,
] as const
}
The useEffect at the bottom always prints undefined, undefined, false
while the call is executed and its results are correct. If the caller instead of relying on the data
, error
intercepts directly the function via then
and catch
it gets the results.
Thank you