我需要在中间件(import { Middleware } from 'redux'
(逻辑中进行翻译,但我不能使用useTranslation
钩子(import { useTranslation } from 'react-i18next'
(。你如何标准地解决这种情况?
错误消息:
React Hook "useTranslation" is called in function "myFunction: Middleware<{}, RootStateType>" which is neither a React function component or a custom React Hook function.
我的代码(缩写(:
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import { Middleware } from 'redux';
import { getType } from 'typesafe-actions';
const scheduleNotificationSettings = (notification: NotificationT): void => {
PushNotificationIOS.scheduleLocalNotification({
fireDate: notification.startDate,
alertTitle: TRANSLATE_SOMEHOW(notification.titleKey),<--- ! HERE ! ¯_( ❛ ͜ʖ ︡❛)_/¯
alertBody: TRANSLATE_SOMEHOW(notification.bodyKey), <--- ! HERE !
alertAction: 'view',
isSilent: false,
repeatInterval: 'week',
});
};
export const handleAction: Middleware<{}, RootStateType> = () => {
return next => {
return action => {
const result = next(action);
switch (action.type) {
case getType(create): {
createNotification(action.payload);
break;
}
case getType(delete): {
removeNotification(action.payload);
break;
}
default:
return result;
}
return result; //pass events to other middleware
};
};
我希望我能正确理解你的问题,但我认为你已经初始化了i18n,所以除了react组件之外,不要使用钩子,只需像这样导入它:
import i18n from 'i18next';
然后使用它:
i18n.t(youri18nKey);
如果您已经在某个地方初始化了i18next,那么您可以像这样使用i18next:
import { t } from "i18next";
const InYourComponent = () => {
t('key')
}
const outside_of_components = () => {
t('key')
}