【问题标题】:NavigationActions.reset is not a function?NavigationActions.reset 不是函数?
【发布时间】:2018-06-10 06:20:22
【问题描述】:

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

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

NavigationActions.reset is not a function

我控制台.log(NavigationActions);

显然没有重置。但是为什么其他人都可以使用该代码?

我想不通。任何帮助,将不胜感激。提前致谢。

这是我的 Welcome.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);

【问题讨论】:

标签: react-native react-navigation


【解决方案1】:

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 的 >2 版本中,您可以使用此代码重置堆栈:

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

我希望这会有所帮助...

【讨论】:

【解决方案2】:

如果您使用的是嵌套路由器,您还需要将 key 设置为 null,否则它将继续查看当前活动的导航器。

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

【讨论】:

  • 感谢您的回复。
  • 即使将键添加到 null,仍在查看当前活动的导航器
  • @AnilGhodake 您的所有导航器也应链接到 1 个通用导航器下。否则图书馆无从得知
  • 我有 3 个堆栈导航器链接在底部选项卡导航器下,使用 createBottomTabNavigator。即使我添加key: null,它仍在查看当前活动的导航器=/
  • 我通过设置 key: MyRouteThatHasTargetScreenNavigationActions.navigate({ routeName: 'TargetScreen'}) 让它工作。希望这会有所帮助!
【解决方案3】:
import { CommonActions,useNavigation } from '@react-navigation/native';

const navigation = useNavigation();

const resetAction = CommonActions.reset({
        index: 0,
        routes: [{ name: 'MainActivity' }],});
      
       navigation.dispatch(resetAction);

document

如果你使用 react-navigation 5.0,你可以这样做

【讨论】:

  • 可以在 React Navigation 版本 6 中使用
【解决方案4】:
navigateToMainActivity(){
this.props.navigation.dispatch(StackActions.reset({
  index:0,
  actions:[
    NavigationActions.navigate({routeName:'MainActivity'})
  ] 
   })
  );
 }

【讨论】:

  • 虽然这可能会回答问题,但您应该 edit 您的回答包括对如何回答问题的解释。对于以后可能遇到相同或类似问题的人来说,仅是代码块的答案不是很有用。
  • 还值得解释为什么它可能比两年前接受的答案更可取。据我所知,这是完全相同的解决方案,但语法略有不同。
【解决方案5】:

在 react-navigation v5 中,您可以像这样用 MainActivity 屏幕替换初始屏幕

import {StackActions} from '@react-navigation/native'; navigation.dispatch(StackActions.replace('MainActivity'));

【讨论】:

    猜你喜欢
    • 2018-08-06
    • 2017-05-06
    • 2019-12-20
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    相关资源
    最近更新 更多