如果没有带有 react-i18next 容器的反应导航的导航道具,我就无法导航



我应用了导航示例,而无需导航prop dect-navigations docs( navigationservice ),但我无法做它可与 react-i18next

一起使用

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

// 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.navigate('Login')),但它返回Navigigationservice.js中的错误_navigator.dispatch is not a function

const navigate = (routeName, params) => {
   // DISPATCH ERROR
   _navigator.dispatch(
     NavigationActions.navigate({
       routeName,
       params
     })
   );
};

依赖项:

  • React 16.5.0
  • 反应原始57.1
  • react-i18Next 9.0.0
  • React-Navigation 3.1.2

有什么建议吗?还有其他人发现这种情况吗?

使用react-i18next的 withNamespaces hoc的 innerRef选项,而不是通过root组件的 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 />
      ...
    );
  };
}

相关内容

  • 没有找到相关文章

最新更新