Basics
Getting StartedCustom Axios Instance
You can define a custom axios instance to be used for a request.
This is useful if your app needs to interact with multiple APIs, each having its own unique set of request options.
# Example
- Define your axios instance (see axios docs):
const instance = axios.create({baseURL: 'https://example/api',authorization: 'Bearer YWxhZGRpbjpvcGVuc2VzYW1l',});
- Pass it to a hook using the
axiosInstance
option:
const { data, error, loading } = useAxios({axiosInstance: instance,url: '/animals/2',});
All requests made will use the provided axios instance.
If you need to use the same instance
across requests that live in different components, create a module that exports the instance:
// apiInstance.jsconst instance = axios.create({baseURL: 'https://example/api',authorization: 'Bearer YWxhZGRpbjpvcGVuc2VzYW1l',});export default instance;