React本机清除Stack Navigator堆栈

时间:2017-09-21 09:42:16

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

我有几个屏幕,我一个接一个地浏览。 Screen1->画面2-screen3-> screen4-首页

我想要的是当我回家时,应该清除以前的导航历史记录,并且后退按钮不应该转到屏幕4的最后一个导航屏幕。当前当我按下主屏幕上的后退按钮时,它需要我回到堆栈中的最后一个路径,即screen4。我使用下面的代码。它给我错误没有路由定义或关键主页。我已在Screens类中定义过。任何帮助将不胜感激。

const resetAction = NavigationActions.reset({
  index: 0,                       
  actions: [NavigationActions.navigate({ routeName: 'Home' })],
});

onPress={() =>  this.props.navigation.dispatch(resetAction)}

6 个答案:

答案 0 :(得分:4)

在V5中,您可以使用

this.props.navigation.reset({
              index: 0,
              routes: [{name: 'Home'}],
            });

答案 1 :(得分:3)

正如react-navigation docs for reset action中所述,索引应该是当前活动路由的索引。错误可能与此有关。

  

如何使用索引参数

     

索引参数用于指定   目前的活跃路线。例如:给出一个带有两个的基本堆栈导航   路线配置文件和设置。要将状态重置为某个点   活动屏幕是设置,但它已堆叠在配置文件的顶部   屏幕,您将执行以下操作:

import { NavigationActions } from 'react-navigation'

const resetAction = NavigationActions.reset({
  index: 1,
  actions: [
    NavigationActions.navigate({ routeName: 'Profile'}),
    NavigationActions.navigate({ routeName: 'Settings'})
  ]
})
this.props.navigation.dispatch(resetAction)

答案 2 :(得分:2)

//copy blobs - from
CloudStorageAccount sourceStorageAccount = new CloudStorageAccount(new StorageCredentials(storageFromName, storageFromKey), true);
CloudBlobClient sourceCloudBlobClient = sourceStorageAccount.CreateCloudBlobClient();
CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference(containerFromName);

//copy blobs - to
CloudStorageAccount targetStorageAccount = new CloudStorageAccount(new StorageCredentials(storageToName, storageToKey), true);
CloudBlobClient targetCloudBlobClient = targetStorageAccount.CreateCloudBlobClient();
CloudBlobContainer targetContainer = targetCloudBlobClient.GetContainerReference(containerToName);

//create target container if didn't exists
try{
    await targetContainer.CreateIfNotExistsAsync();
}
catch(Exception e){
    log.Error(e.Message);
}

CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(blobName);

try{
    //initialize copying
    await targetBlob.StartCopyAsync(sourceBlob.Uri);
}
catch(Exception ex){
    log.Error(ex.Message);
    //return error, in my case HTTP
    return req.CreateResponse(HttpStatusCode.BadRequest, "Error, source BLOB probably has private access only: " +ex.Message);
} 

//fetch current attributes
targetBlob.FetchAttributes();

//waiting for completion
while (targetBlob.CopyState.Status == CopyStatus.Pending){
    log.Info("Status: " + targetBlob.CopyState.Status);
    Thread.Sleep(500);
    targetBlob.FetchAttributes();
}

//check status
if (targetBlob.CopyState.Status != CopyStatus.Success){
    //return error, in my case HTTP
    return req.CreateResponse(HttpStatusCode.BadRequest, "Copy failed with status: " + targetBlob.CopyState.Status);
}

//finally remove source in case Copy Status was Success
sourceBlob.Delete();

//and return success (in my case HTTP)
return req.CreateResponse(HttpStatusCode.OK, "Done.");

你可以使用它,这对我有用..

答案 3 :(得分:1)

答案是 createSwitchNavigator ,它不会堆叠您的导航。 在具有主屏幕/堆栈的createSwitchNavigator中添加您的身份验证屏幕/导航器。

因此,当您从家中导航以登录时,堆栈不会保留。

更多信息 https://reactnavigation.org/docs/en/auth-flow.html

答案 4 :(得分:0)

//导入

从“反应导航”中导入{NavigationActions,StackActions};

//重置当前导航堆栈并创建新的导航堆栈

const loginScreenAction = StackActions.reset({ 索引:0,
操作:[NavigationActions.navigate({routeName:'LoginScreen'})], });

///使用新的导航堆栈打开新组件

this.props.navigation.dispatch(loginScreenAction)

答案 5 :(得分:0)

使用 @react-navigation 5.x 您可以使用 CommonActions 重置/清除堆栈。我会在这里留下一个例子。

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

const handleSigninNavigation = () => {
 navigation.dispatch(
  CommonActions.reset({
    index: 0,
    routes: [{ name: "your-new-route" }]
  }));
}

您也可以在重置堆栈时创建自己的堆栈。只需将路由的名称传递给 routes 数组并给出一个索引以首先导航。

CommonActions.reset({
  index: 1,
  routes: [{ name: "home" }, { name: "signin" }]
}));

以上代码将使用给定路由重置当前堆栈,并导航到 signin,因为我们将索引设置为 1。