如果没有react-i18next容器的react-navigation导航道具,我将无法导航

时间:2019-02-16 19:38:40

标签: react-native react-navigation react-i18next

我在没有react-navigations文档( NavigationService )的导航道具的情况下应用了导航示例,但是我无法使其与 react-一起使用i18next

我在代码中应用了文档https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html的示例:

fill_dict

当验证了授权令牌并且结果为负数时,必须打开登录屏幕(// App.js import React from 'react'; import { Provider } from 'react-redux'; import { PersistGate } from 'redux-persist/lib/integration/react'; import { createAppContainer } from 'react-navigation'; import { I18nextProvider, withNamespaces } from 'react-i18next'; import { persistor, store } from './src/store'; import I18n from './src/localization'; import Navigation from './src/navigation'; import NavigationService from './src/navigation/NavigationService'; import Loader from './src/screens/components/Loader'; class NavigationStack extends React.Component { static router = Navigation.router; render() { const { t } = this.props; return <Navigation screenProps={{ t, I18n }} {...this.props} />; } }; const ReloadNavOnLanguageChange = withNamespaces(['common', 'server'], { bindI18n: 'languageChanged', bindStore: false, })(createAppContainer(NavigationStack)); export default class App extends React.Component { ... render() { return ( <Provider store={store}> <PersistGate loading={<Loader />} persistor={persistor}> <I18nextProvider i18n={ I18n } > <ReloadNavOnLanguageChange ref={navigatorRef => { NavigationService.setTopLevelNavigator(navigatorRef); }} /> </I18nextProvider> </PersistGate> </Provider> ); }; }; // Navigation.js ... export default Navigation = createSwitchNavigator( { AuthLoading: AuthLoadingScreen, Login: LoginScreen, App: AppScreen }, { initialRouteName: 'AuthLoading' } ); // NavigationService.js Apply the same code that's in https://reactnavigation.org/docs/en/navigating-without-navigation-prop.html // Any JS module of my project (actions, helpers, components, etc.) import NavigationService from 'path-to-NavigationService.js'; ... NavigationService.navigate('Login'); ),但它会在NavigationService.js中返回错误NavigationService.navigate('Login')

_navigator.dispatch is not a function

依赖项:

  • 反应16.5.0
  • 本机57.1
  • react-i18next 9.0.0
  • 反应导航3.1.2

有什么建议吗?还有其他人找到这种情况吗?

1 个答案:

答案 0 :(得分:0)

使用react-i18next的innerRef hoc的withNamespaces选项,而不是通过根组件的ref属性传递函数……就像REACT-I18NETX的文档所述!

// App.js

...
const ReloadNavOnLanguageChange = withNamespaces(['common', 'server'], {
  bindI18n: 'languageChanged',
  bindStore: false,
  innerRef: (ref) => NavigationService.setTopLevelNavigator(ref)
})(createAppContainer(NavigationStack));

export default class App extends React.Component {
  ...
  render() {
    return (
      ...
      <ReloadNavOnLanguageChange />
      ...
    );
  };
}