I need ot use useSelector
in a axios interceptor function to get user name. I have tryed to do in a custom hook but getting same error
Any ideas how to do that?
currently Im getting:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
my code:
const instance: AxiosInstance = axios.create();
axiosInterceptor(instance);
function axiosInterceptor(instance: AxiosInstance): any {
return instance.interceptors.request.use(
(request) => axiosRequest(request),
(error) => axiosRequestError(error?.message, error)
);
}
function axiosRequest(
request: AxiosRequestConfig
): AxiosRequestConfig | Promise<AxiosRequestConfig> {
const currentToken = useSelector(currentUserToken);
request.headers["X-Auth-token"] = currentToken;
return request;
}
function axiosRequestError(message: string, error?: any): Promise<any> {
return Promise.reject(error || message);
}