我创建了一个有Welcome个屏幕的项目,导航到MainActivity个屏幕.我希望当用户点击后退按钮时,它会在MainActivity而不是Welcome屏幕上关闭应用程序.我使用react-navigation库,所以我从Github中寻找一些解决方案.

当我使用https://github.com/react-navigation/react-navigation/issues/295的代码时.我得到了一个错误:

NavigationActions.reset is not a function

我安慰他.日志(log)(导航操作);

enter image description here

显然没有重置.但是为什么其他人都能使用代码呢?

我搞不懂.任何帮助都将不胜感激.提前谢谢.

这是我的欢迎.js:

import React, { Component } from 'react';
import { View, Text, ActivityIndicator } from 'react-native';
import { NavigationActions } from 'react-navigation';
import { connect } from 'react-redux';
import { ColorSetting } from './common/ColorSetting';
import { fetchMainMovieList } from '../actions';

class Welcome extends Component {
  static navigationOptions = {
    header: null,
  };

  componentDidMount() {
    // call main page data first
    this.props.fetchMainMovieList();

    this.timer = setTimeout(() => { 
      this.navigateToMainActivity();
    }, 3000);
  }
  componentWillUnmount() {
    // if this.timer existed,then use clearTimeout to remove it.
    this.timer && clearTimeout(this.timer);
  }

  navigateToMainActivity() {
    console.log(NavigationActions);
    const resetAction = NavigationActions.reset({
      index: 1,
      actions: [
        NavigationActions.navigate({ routeName: 'MainActivity' })
      ]
    });

    this.props.navigation.dispatch(resetAction);
  }

  render() {
    return (
      <View>
        <Text>Three !</Text>
      </View>
    );
  }
}

export default connect(null, { fetchMainMovieList })(Welcome);

推荐答案

Update for V5.x:

import { CommonActions } from '@react-navigation/native';


const resetAction = CommonActions.reset({
    index: 1,
    routes: [{ name: YOUR_ROUTE_NAME, params: { YOUR_OPTIONAL_DATA } }]
});
navigation.dispatch(resetAction);

在版本中>;在react navigation中,您可以使用以下代码重置堆栈:

    import { NavigationActions, StackActions } from 'react-navigation';
    const resetAction = StackActions.reset({
            index: 0,
            actions: [NavigationActions.navigate({ routeName: 'MainActivity' })],
        });
    this.props.navigation.dispatch(resetAction);

我希望这会有帮助...

React-native相关问答推荐

react 本机中的TextInput对齐中的长文本

何时在 React Native 中使用辅助功能 Role='link'?

将屏幕标题更新为所选标签导航的标题

如何 break .map 功能

React Native 获取视图相对于屏幕的绝对位置

TypeError:undefined is not an object(判断'navigator.geolocation.requestAuthorization')

react-native app.js index.js

我可以在 react-native 中使用 reactJS 库吗?

如何从 React Native-App 中的 res/drawable-folder 加载图像?

自定义原生基础的选项卡

react native如何访问文件系统?

Android 构建错误AndroidManifest.xml requires a placeholder substitution

在 React Native (iOS) 中支持动态类型

如何在 React Native 中优化图像

我可以从react-native元素中删除 tvOS 吗?

列之间的 React Native FlatList 分隔符

为什么我收到警告:函数作为 React 子级无效?

加载字体native-base 错误

React Native:文本 node 的 Auto宽度

在 React Native 中,两个等宽的按钮水平填充屏幕