I am trying to create an axios instance that I can use throughout the app. I used this same pattern in Vue using Vuex store but not sure how to do this in React. I have an AuthProvider for AuthContext and would like to useContext when creating the axios instance but I'm receiving an error that I can't use useContext outside of a component.
What is the best way to accomplish this? I have the user and a logout function in the AuthContext that I would like to use but this pattern may not be best for React.
import React, { useContext } from "react";
import axios from 'axios'
import { AuthContext } from "./AuthProvider";
import { getEnvironment } from "./environment";
const env = getEnvironment();
const { user, logout } = useContext(AuthContext);
const instance = axios.create();
instance.defaults.baseURL = env.apiUrl;
instance.defaults.headers.common['Authorization'] = `Bearer ${user.token}`;
instance.interceptors.response.use(function (response) {
return response
}, function (error) {
if (typeof error.response === 'undefined' || [401, 419].includes(error.response.status)) {
logout();
}
return Promise.reject(error)
})
export default instance;