React Navigator StackNavigator:当从同一场景中调用两次时,goBack不起作用 [英] React Navigator StackNavigator: goBack does not work when called twice from the same scene

查看:86
本文介绍了React Navigator StackNavigator:当从同一场景中调用两次时,goBack不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我想用目录结构实现文件浏览器.我创建了一个React Native组件,其中列出了特定文件夹的文件和目录.当我单击一个文件夹时,我希望进入新文件夹并列出其文件和文件夹.显然,我希望能够使用相同的React组件来呈现不同的文件夹.

Let's assume I want to implement a file explorer with directory structure. I created a React Native component that lists files and directories of a specific folder. When I click on a folder, I expect to go inside the new folder and list its files and folders. Obviously, I am hoping to be able to use the same React component to render the different folders.

我使用 StackNavigator .这是我的代码

这是我的尝试,不起作用:

Here is my attempt which does not work:

import React, { Component } from 'react';
import {
  AppRegistry,
  BackHandler,
  ListView,
  StyleSheet,
  Text,
  TouchableNativeFeedback,
  View
} from 'react-native';

import {
  StackNavigator,
} from 'react-navigation';

export default class FileExplorerScene extends Component {
  // Initialize the hardcoded data
  constructor(props) {
    super(props);

    // Empty list before adding entries
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2 });
    this.state = {
      dataSource: ds.cloneWithRows(['a', 'b', 'c'])
    };
  }

  onPress(rowData) {
    console.log("onPress(" + rowData + ")");
    this.props.navigation.navigate('FileExplorer', { parent: rowData });
  }

  goBack() {
    this.props.navigation.goBack();
    return true;
  }

  componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress', this.goBack.bind(this));
  }

  renderRow(rowData, sectionID, rowID) {
    return (
      <TouchableNativeFeedback onPress={ () => { this.onPress(rowData); }}>
        <Text style={{ margin:5, fontSize: 20}}>{ rowData }</Text>
      </TouchableNativeFeedback>
      );
  }

  render() {
    const { params } = this.props.navigation.state;

    var currentPath;
    if (params) {
      currentPath = (<Text style={{ margin:10, fontSize: 24}}>Current Path: {params.parent}</Text>);
    } else {
      currentPath = (<Text style={{ margin:10, fontSize: 24}}>Root path</Text>);
    }

    return (
      <View>
        { currentPath }
        <ListView
          dataSource={this.state.dataSource}
          renderRow={this.renderRow.bind(this)}
          enableEmptySections={true}
        />
      </View>
    );
  }
}

const App = StackNavigator(
  {
    FileExplorer: { screen: FileExplorerScene },
  },{
    headerMode: 'none',
  }
);

AppRegistry.registerComponent('ReactProject', () => App);

  • 单击a:我可以看到Current Path: a

    I/ReactNativeJS( 1125): onPress(a)
    I/ReactNativeJS( 1125): 'Navigation Dispatch: ', { action: 
    I/ReactNativeJS( 1125):    { type: 'Navigation/NAVIGATE',
    I/ReactNativeJS( 1125):      routeName: 'FileExplorer',
    I/ReactNativeJS( 1125):      params: { parent: 'a' },
    I/ReactNativeJS( 1125):      action: undefined },
    I/ReactNativeJS( 1125):   newState: 
    I/ReactNativeJS( 1125):    { index: 1,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 1125):   lastState: 
    I/ReactNativeJS( 1125):    { index: 0,
    I/ReactNativeJS( 1125):      routes: [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' } ] } }
    

  • 单击b:我可以看到Current Path: b

    I/ReactNativeJS( 1125): onPress(b)
    I/ReactNativeJS( 1125): 'Navigation Dispatch: ', { action: 
    I/ReactNativeJS( 1125):    { type: 'Navigation/NAVIGATE',
    I/ReactNativeJS( 1125):      routeName: 'FileExplorer',
    I/ReactNativeJS( 1125):      params: { parent: 'b' },
    I/ReactNativeJS( 1125):      action: undefined },
    I/ReactNativeJS( 1125):   newState: 
    I/ReactNativeJS( 1125):    { index: 2,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'b' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-2',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 1125):   lastState: 
    I/ReactNativeJS( 1125):    { index: 1,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] } }
    

  • 单击c:我可以看到Current Path: c

    I/ReactNativeJS( 1125): onPress(c)
    I/ReactNativeJS( 1125): 'Navigation Dispatch: ', { action: 
    I/ReactNativeJS( 1125):    { type: 'Navigation/NAVIGATE',
    I/ReactNativeJS( 1125):      routeName: 'FileExplorer',
    I/ReactNativeJS( 1125):      params: { parent: 'c' },
    I/ReactNativeJS( 1125):      action: undefined },
    I/ReactNativeJS( 1125):   newState: 
    I/ReactNativeJS( 1125):    { index: 3,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'b' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-2',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'c' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-3',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 1125):   lastState: 
    I/ReactNativeJS( 1125):    { index: 2,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'b' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-2',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] } }
    

  • 返回.我可以看到Current Path: b

    I/ReactNativeJS( 1125): goBack()
    I/ReactNativeJS( 1125): 'Navigation Dispatch: ', { action: { type: 'Navigation/BACK', key: 'id-1497310673011-3' },
    I/ReactNativeJS( 1125):   newState: 
    I/ReactNativeJS( 1125):    { index: 2,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'b' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-2',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 1125):   lastState: 
    I/ReactNativeJS( 1125):    { index: 3,
    I/ReactNativeJS( 1125):      routes: 
    I/ReactNativeJS( 1125):       [ { routeName: 'FileExplorer', key: 'Init-id-1497310673011-0' },
    I/ReactNativeJS( 1125):         { params: { parent: 'a' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-1',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'b' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-2',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 1125):         { params: { parent: 'c' },
    I/ReactNativeJS( 1125):           key: 'id-1497310673011-3',
    I/ReactNativeJS( 1125):           routeName: 'FileExplorer' } ] } }
    

  • 返回.没变化

  • Go Back. No change

    I/ReactNativeJS( 1125): goBack()
    

  • 返回.没变化

  • Go Back. No change

    I/ReactNativeJS( 1125): goBack()
    

  • 我可以正确使用goBack吗?还是StackNavigator中的问题?

    Do I use goBack correctly? Or it is an issue in StackNavigator?

    推荐答案

    实际上将this.props.navigation.goBack()替换为this.props.navigation.goBack(null)使其起作用!
    反应导航文档非常令人困惑...

    Actually replacing this.props.navigation.goBack() by this.props.navigation.goBack(null) makes it work!
    React Navigation documentation is quite confusing...

    现在,一切都按我的意愿进行:

    Now, everything works as I wanted to be:

    • 单击a:我可以看到Current Path: a

    I/ReactNativeJS( 2045): onPress(a)
    I/ReactNativeJS( 2045): 'Navigation Dispatch: ', { action: 
    I/ReactNativeJS( 2045):    { type: 'Navigation/NAVIGATE',
    I/ReactNativeJS( 2045):      routeName: 'FileExplorer',
    I/ReactNativeJS( 2045):      params: { parent: 'a' },
    I/ReactNativeJS( 2045):      action: undefined },
    I/ReactNativeJS( 2045):   newState: 
    I/ReactNativeJS( 2045):    { index: 1,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 2045):   lastState: 
    I/ReactNativeJS( 2045):    { index: 0,
    I/ReactNativeJS( 2045):      routes: [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' } ] } }
    

  • 单击b:我可以看到Current Path: b

  • Click on b: I can see Current Path: b

    I/ReactNativeJS( 2045): onPress(b)
    I/ReactNativeJS( 2045): 'Navigation Dispatch: ', { action: 
    I/ReactNativeJS( 2045):    { type: 'Navigation/NAVIGATE',
    I/ReactNativeJS( 2045):      routeName: 'FileExplorer',
    I/ReactNativeJS( 2045):      params: { parent: 'b' },
    I/ReactNativeJS( 2045):      action: undefined },
    I/ReactNativeJS( 2045):   newState: 
    I/ReactNativeJS( 2045):    { index: 2,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'b' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-2',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 2045):   lastState: 
    I/ReactNativeJS( 2045):    { index: 1,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] } }
    

  • 单击c:我可以看到Current Path: c

  • Click on c: I can see Current Path: c

    I/ReactNativeJS( 2045): onPress(c)
    I/ReactNativeJS( 2045): 'Navigation Dispatch: ', { action: 
    I/ReactNativeJS( 2045):    { type: 'Navigation/NAVIGATE',
    I/ReactNativeJS( 2045):      routeName: 'FileExplorer',
    I/ReactNativeJS( 2045):      params: { parent: 'c' },
    I/ReactNativeJS( 2045):      action: undefined },
    I/ReactNativeJS( 2045):   newState: 
    I/ReactNativeJS( 2045):    { index: 3,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'b' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-2',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'c' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-3',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 2045):   lastState: 
    I/ReactNativeJS( 2045):    { index: 2,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'b' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-2',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] } }
    

  • 返回.我可以看到Current Path: b

    I/ReactNativeJS( 2045): goBack()
    I/ReactNativeJS( 2045): 'Navigation Dispatch: ', { action: { type: 'Navigation/BACK', key: null },
    I/ReactNativeJS( 2045):   newState: 
    I/ReactNativeJS( 2045):    { index: 2,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'b' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-2',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 2045):   lastState: 
    I/ReactNativeJS( 2045):    { index: 3,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'b' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-2',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'c' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-3',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] } }
    

  • 返回.我可以看到Current Path: a

    I/ReactNativeJS( 2045): goBack()
    I/ReactNativeJS( 2045): 'Navigation Dispatch: ', { action: { type: 'Navigation/BACK', key: null },
    I/ReactNativeJS( 2045):   newState: 
    I/ReactNativeJS( 2045):    { index: 1,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] },
    I/ReactNativeJS( 2045):   lastState: 
    I/ReactNativeJS( 2045):    { index: 2,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' },
    I/ReactNativeJS( 2045):         { params: { parent: 'b' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-2',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] } }
    

  • 返回.我可以看到Root path

    I/ReactNativeJS( 2045): goBack()
    I/ReactNativeJS( 2045): 'Navigation Dispatch: ', { action: { type: 'Navigation/BACK', key: null },
    I/ReactNativeJS( 2045):   newState: 
    I/ReactNativeJS( 2045):    { index: 0,
    I/ReactNativeJS( 2045):      routes: [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' } ] },
    I/ReactNativeJS( 2045):   lastState: 
    I/ReactNativeJS( 2045):    { index: 1,
    I/ReactNativeJS( 2045):      routes: 
    I/ReactNativeJS( 2045):       [ { routeName: 'FileExplorer', key: 'Init-id-1497998297948-0' },
    I/ReactNativeJS( 2045):         { params: { parent: 'a' },
    I/ReactNativeJS( 2045):           key: 'id-1497998297948-1',
    I/ReactNativeJS( 2045):           routeName: 'FileExplorer' } ] } }
    

  • 返回.出乎意料的:-)

  • Go Back. Nothing as expected :-)

    I/ReactNativeJS( 2045): goBack()
    

  • 这篇关于React Navigator StackNavigator:当从同一场景中调用两次时,goBack不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆