Answer the question
In order to leave comments, you need to log in
How to use context in a request to the server?
Hello!
I store user settings in a React context. I want to take the language and currency from the user's settings and substitute this data in the API request. The code after many attempts looks like this (TypeScript):
const useSettings = () => {
return useContext(SettingsContext)
}
export const ApiFetch = async (url: string, endpoint: string, data: any = {}) => {
const settings = useSettings();
data['language'] = settings.language
data['currency'] = settings.currency
const promise = axios.get(url + endpoint, {params: data});
return promise.then((response) => response.data)
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question