简体   繁体   English

未通过反应导航呈现嵌套屏幕

[英]Not rendered nested screen by react-navigation

While I have a flat navigation like this 虽然我有这样的平面导航

const MainNavigator = TabNavigator({
  Map: { 
    screen: MapScreen
  },
  RoadObject: { 
    screen: RoadObjectScreen
  }
});

class RoadObjectScreen is rendered every time when it is navigated to. 每次导航到RoadObjectScreen类时,都会渲染该类。

class RoadObjectScreen extends Component {

static navigationOptions = ({ navigation }) => ({
    title: navigation.state.params && navigation.state.params.roadObject,
    tabBarVisible: false
});

render() {
    console.log('Navigation:', this.props.navigation);
    const { navigation } = this.props;
    return (
        <View>
            <Text style={{ paddingTop: 100 }} onPress={() => navigation.goBack()}>
                Welcome to {navigation.state.params && navigation.state.params.roadObject }
            </Text>
        </View>
    );
}

} }

BUT when I get screen nested, it stops to be rendered, just one at start of application. 但是,当我将屏幕嵌套时,它将停止渲染,仅在应用程序启动时显示一个。 So console.log('Navigation:', this.props.navigation); 所以console.log('Navigation:',this.props.navigation); popups just once. 弹出窗口一次。

const MainNavigator = TabNavigator({

  Map: { 
    screen: MapScreen, 
    navigationOptions: {
      tabBarVisible: false
    }
  },
  RoadObjectMain: {
    screen: StackNavigator({
      RoadObject: { 
        screen: RoadObjectScreen
      }
    }) 
  }
});

Why does it happen? 为什么会发生? How get I get it rendered and get this.props.navigation accessable. 我该如何渲染它并获得this.props.navigation可访问性。 Thank you! 谢谢!

In TabNavigator , tabs render once when the navigator is mounted so when you switch between tabs the screens will not be rendered a second time. TabNavigator ,安装导航器后,选项卡仅渲染一次,因此在选项卡之间切换时,不会再次渲染屏幕。

Although they are not rendering second time you can still access this.props.navigation in your class. 尽管它们不是第二次渲染,您仍然可以在类中访问this.props.navigation

If you want your tabs to be rendered only when you switch to that tab, you can use lazy prop. 如果您希望仅在切换到该选项卡时才呈现这些选项卡,则可以使用lazy道具。 This will make your tabs render when they are switched but they will be rendered only once unless there is any change in state or props for that screen. 这将使您的标签页在切换时呈现,但除非该屏幕的状态或道具发生任何变化,否则它们仅会呈现一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM