I have following hook
import axios from "axios";
import {useKeycloak} from "@react-keycloak/web";
import {useEffect, useState} from "react";
export const useAdminApi = () => {
const {keycloak} = useKeycloak();
const [axiosInstance, setAxiosInstance] = useState(undefined);
useEffect(() => {
let instance = axios.create({
baseURL: `${process.env.REACT_APP_ADMIN_API_URL}`,
headers: {
Test: 'test',
Authorization: 'Bearer ' + keycloak.token,
}
});
setAxiosInstance(instance);
return () => {
setAxiosInstance(undefined);
}
}, [keycloak.token]);
const getUsers = ({query}) => {
return axiosInstance.get(`/users${query}`)
};
const getUserDetail = ({userId}) => {
return axiosInstance.get(`/users/${userId}`)
};
const deleteUser = ({userId}) => {
return axiosInstance.delete(`/users/${userId}`)
};
return {
getUsers,
getUserDetail,
deleteUser
}
};
When I log instance it's logged with all config
From useAdminApi I'd like to export functions like getUserDetail, deleteUser, ...
Then in other component, I'd like to use this functions so I have following:
const UserForm = () => {
const {getUserDetail} = useAdminApi();
useEffect(() => {
if (!userId) {
setIsNew(true);
} else {
setIsNew(false);
getUserDetail({userId})
.then(result => setUserData(result.data))
.catch(error => pushError(push, error));
}
}, [userId]);
...
}
However, when I display the UserForm I'm getting following error: TypeError: Cannot read property 'get' of undefined which is pointing to this line return axiosInstance.get(`/users/${userId}`)
Can somebody please tell me what's wrong with this approach?