I'm using i18next library to implement localisation in my react app, where I've to call an API to get the localisation in a response. But I want to use react query to call an API, and I know that react query being a hook can only be called in functional component, so I'm unable to use react query. Did anyone faced this error?
My i18next configuration.
export const getLocalisation = async (lng: string) => {
try {
const localisationResponse = await axios.get(
`/localisation/${lng}`
);
return localisationResponse;
} catch (err) {
console.log('Error at getLocalisation: ', err);
}
};
const backendOptions = {
allowMultiLoading: true,
loadPath: `/localisation/en`,
request: (options: any, url: any, payload: any, callback: any) => {
try {
getLocalisation(EN).then((result) => {
callback(null, {
data: result?.data,
status: STATUS_CODE_200
});
});
} catch (e) {
console.error(e);
callback(e); // Passing `e` will handle error status code
}
}
};
i18n
.use(HttpBackend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
debug: false,
fallbackLng: EN,
supportedLngs: [EN],
ns: [],
partialBundledLanguages: true,
interpolation: {
escapeValue: false
},
backend: backendOptions
});
export default i18n;
When using react query, getting following error/warning. enter image description here
You could to something like this:
then in your index or where you load your app component:
and then you can just use the
hook where you want.