E
E
Evgeny_A2022-03-16 16:15:08
React
Evgeny_A, 2022-03-16 16:15:08

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)

};

Swears that:

Uncaught (in promise) Error: Invalid hook call.

I read the docks, I watched the lessons, but I can't start it. Can you suggest why such access to the context does not work? Or maybe somehow you can store user settings in the browser to use them in API requests?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question