简体   繁体   English

如何从外部设置初始标签

[英]How to set the initial tab from outside

Consider the scenes below: 考虑以下场景:

<Scene key='home' component={HomeComponent} hideNavBar title={'home'}/>
<Scene key="myTabBar" tabs={true} hideNavBar tabBarStyle={style.tabBarStyle}>
    <Scene 
        key="myTab" 
        title="My Tab" 
        icon={MyTabIcon} 
        component={MyTabComponent} hideNavBar />
    <Scene 
        key="myTab_1" 
        title="My Tab 1" 
        icon={MyTabIcon} 
        component={MyTabComponent1} hideNavBar />
</Scene>

I have two buttons in HomeComponent, Button1 and button Button2. 我在HomeComponent中有两个按钮,Button1和Button2。

I want to show myTab_1 when Button1 clicked, and show myTab_2 when Button2 clicked. 我想说明myTab_1时Button1的点击,并显示myTab_2时Button2的点击。 How can i achieve this? 我怎样才能做到这一点?

Actually its quite simple, but there is no documentation or issue discussed. 实际上它非常简单,但是没有讨论文档或问题。 To evaluate this, we can look at the source code of TabBar in repo src directory. 为了对此进行评估,我们可以在repo src目录中查看TabBar源代码

... (imports)

class TabBar extends Component {

  ...

  static onSelect(el) {
    if (!Actions[el.props.name]) {
      throw new Error(
        `No action is defined for name=${el.props.name} ` +
        `actions: ${JSON.stringify(Object.keys(Actions))}`);
    }
    if (typeof el.props.onPress === 'function') {
      el.props.onPress();
    } else {
      Actions[el.props.name]();
    }
  }
}

When we click on the tab itself, we actually invoke the function onSelect (take a look at render part) which simply routes using Action[tabbarbutton.props.name]() 当我们点击标签本身时,我们实际上调用了onSelect函数(看一下渲染部分),该函数只是使用Action[tabbarbutton.props.name]()路由

For your code its Action.myTab_1() in your TouchableOpacity. 对于您的代码,其在Action.myTab_1()

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

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