【问题标题】:navigation navigate outside appcontainer in app.js在 app.js 中导航到 appcontainer 外部
【发布时间】:2020-08-19 15:42:52
【问题描述】:

当前行为

我使用反应导航 v4x undefined 不是对象 navigation.discpatch

预期行为

我想在 appcontainer 中导航登录页面 `

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {isFirstLaunch: false, hasCheckedAsyncStorage: false};
    const myRef = createRef();
  }

  onPressLogin() {
    this.navigator.dispatch(
      NavigationActions.navigate({ routeName: 'Login' })
    );
  }
  async componentDidMount() {
    const isFirstLaunch = await checkIfFirstLaunch();
    this.setState({isFirstLaunch, hasCheckedAsyncStorage: true});
  }

  render() {
    const {hasCheckedAsyncStorage, isFirstLaunch} = this.state;

    if (!hasCheckedAsyncStorage) {
      return null;
    }
    return isFirstLaunch ? (
      <EntryInfo onPress={this.onPressLogin} />
    ) : (
      <AppContainer
        ref={nav => {
          setTopLevelNavigator(nav);
          this.navigator = nav;
        }}
      />
    );
  }
}`

软件版本安卓 反应导航 4.4.0 反应导航堆栈 2.8.2 反应原生 0.62.2

【问题讨论】:

标签: react-native react-navigation react-navigation-stack


【解决方案1】:

在导航之前确保导航器存在。它适用于 m

function setTopLevelNavigator(navigatorRef) {
if(navigatorRef)
{
   navigator = navigatorRef;
}
  
}

function navigate(routeName, params) {
  if(navigator && routeName){
   navigator.dispatch(
    NavigationActions.navigate({
      routeName,
      params,
    })
  );
  }
  
}

// add other navigation functions that you need and export them

export default {
  navigate,
  setTopLevelNavigator,
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 2022-08-24
    • 2013-11-04
    • 1970-01-01
    相关资源
    最近更新 更多