• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在另一个 React 组件中正确使用 TabNavigator

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:25:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

问题 - 如何在另一个充当包装器组件的 React 组件中正确使用 React Navigation 的 TabNavigator 容器组件?

我想要实现的目标 - 基本上我希望 appbar 和 tabbar 都显示 - appbar 在顶部,tabbar (TabBarTop) 就在它下面,这是一种非常常见的设计模式。

我尝试了几种方法。

方法#1(嵌套在 StackNavigator 中)

Tab.js

export const Tab = TabNavigator({
    Tab1: { screen: Tab1Container },
    Tab2: { screen: Tab2Container }
  }, {
    tabBarComponent: TapBarTop,
    tabBarPosition: 'top'
  });

AppBar.js

class AppBarComponent extends Component {
  static navigationOptions = { header: null }
  render() {
    return (
      <View>
        *some more views, buttons blah blah here
      </View>
    )
  }
}
export default AppBarComponent;

我在 StackNavigator 中使用它们,比如

export default StackNavigator({
  stack1: { screen: AppBarComponent },
  stack2: { screen: Tab }
});

这导致在给定时间仅显示 1 个堆栈,这正是它的工作原理。而且我与 initialRouteName 没有任何关系。

方法#2(包装在另一个组件中)

<View style={{ flex: 1 }}>
  <AppBarComponent />
  <Tab />
</View>

这相反显示两个组件,但 this.props.navigation.navigate('somepath')push()pop()replace() 在内部和 .但是 this.props.navigation 及其方法都可以在这些组件中使用。

PS - 我正在使用 React Navigation v1 并在 iOS 上运行



Best Answer-推荐答案


方法一的解决方案

拥有一个只有一个屏幕的 StackNavigator,并在该屏幕中显示您的 TabNavigator。然后自定义header使用您的自定义 AppBarComponent

header

React Element or a function that given HeaderProps returns a React Element, to display as a header. Setting to null hides header.

示例

export default StackNavigator({
  stack: { 
    screen: Tab,
    navigationOptions: {
      header: (HeaderProps) => (<AppBarComponent headerProps={HeaderProps} />)
    }
  }
});

方法2的解决方案

您可以使用 withNavigation 包装不属于堆栈的组件HOC。

withNavigation is a higher order component which passes the navigation prop into a wrapped Component. It's useful when you cannot pass the navigation prop into the component directly, or don't want to pass it in case of a deeply nested child.

示例

class AppBarComponent extends Component {
  static navigationOptions = { header: null }
  render() {
    return (
      <View>
        *some more views, buttons blah blah here
      </View>
    )
  }
}
export default withNavigation(AppBarComponent);

关于ios - 如何在另一个 React 组件中正确使用 TabNavigator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926323/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap