gpt4 book ai didi

react-native - NavigationActions.reset 不是函数吗?

转载 作者:行者123 更新时间:2023-12-03 12:21:07 25 4
gpt4 key购买 nike

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

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

NavigationActions.reset is not a function

我 console.log(NavigationActions);

enter image description here

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

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

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

最佳答案

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);

在版本> 2的 react 导航中,您可以使用此代码重置堆栈:
    import { NavigationActions, StackActions } from 'react-navigation';
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'MainActivity' })],
});
this.props.navigation.dispatch(resetAction);
我希望这个能帮上忙...

关于react-native - NavigationActions.reset 不是函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50781080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com