假设我在Profile3
。我想重置为Stack1
(底部导航器的初始路径)。那怎么可能?我只能重设为Stack3
。但不是Stack1
。
如果我尝试将Stack3
替换为Stack1
或Bottom1
,它将出现错误消息。看来stackreset必须来自同一堆栈导航器?
错误:没有为键Stack1定义任何路由。必须是以下之一 Stack3,Profile3
重置密码
import { NavigationActions } from 'react-navigation';
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Stack3' })],
});
this.props.navigation.dispatch(resetAction);
导航代码:
const Stack1 = createStackNavigator(
{
Stack1: Home1,
Profile1: Profile
}
);
const Stack2 = createStackNavigator(
{
Stack2: Home2,
Profile2: Profile
}
);
const Stack3 = createStackNavigator(
{
Stack3: Home3,
Profile3: Profile
}
);
const AppNavigator = createBottomTabNavigator({
Bottom1: Stack1,
Bottom2: Stack2,
Bottom3: Stack3
}, {
{
initialRouteName: 'Bottom1',
});
export default createAppContainer(AppNavigator);
答案 0 :(得分:0)
import { NavigationActions } from 'react-navigation';
const resetAction = NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Stack1 ' })],
});
this.pr
ops.navigation.dispatch(resetAction);
如果您想重置1,则将Stack3
替换为Stack1
,并在分派时从索引0加载该堆栈
编辑:
由于我们需要将主堆栈重置为我们需要使用NavigationActions
而不是StackActions
来进行重置
让我知道是否可行
答案 1 :(得分:0)
尝试在要重置堆栈的任何位置上用stack1替换重置中的stack3:
import { NavigationActions } from 'react-navigation';
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Stack1' })],
});
this.props.navigation.dispatch(resetAction);
更新:
找到了修复程序,只需要在props.getLastPosition()上使用navigationState.index并在分发之前添加检查以查看哪个选项卡是当前的。
tabBarComponent: props => {
const {navigation, navigationState} = props
const jumpToIndex = index => {
const lastPosition = navigationState.index
const tab = navigationState.routes[index]
const tabRoute = tab.routeName
const firstTab = tab.routes[0].routeName
lastPosition !== index && navigation.dispatch(pushNavigation(tabRoute))
lastPosition === index && navigation.dispatch(resetNavigation(firstTab))
}
return <TabView.TabBarBottom {...props} jumpToIndex={jumpToIndex}/>
},
一次检查此链接rn-issue
希望有帮助。毫无疑问