如何从外部设置初始选项卡

时间:2017-03-07 12:41:55

标签: javascript react-native react-native-router-flux

考虑以下场景:

<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>

我在HomeComponent,Button1和Button2按钮中有两个按钮。

我想在点击Button1时显示myTab_1,并在点击Button2时显示myTab_2。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

实际上它很简单,但没有讨论过的文档或问题。为了评估这一点,我们可以查看repo src目录中的source code of TabBar

    SELECT t4.id, t4.username, t4.avatar_url, p_count * 50 + ue_count * 2 + fav_count * 10 + ep_count * 2 + COUNT(vp.id) * 2 as point
    FROM (SELECT t3.id, t3.username, t3.avatar_url, p_count, ue_count, fav_count, COUNT(ep.id) as ep_count
          FROM( SELECT t2.id, t2.username, t2.avatar_url, p_count, ue_count, COUNT(fav_p.id) as fav_count
                FROM (SELECT t1.id, t1.username, t1.avatar_url, p_count, COUNT(e.user_id) as ue_count
                      FROM (SELECT u.*, COUNT(p.user_id) as p_count
                            FROM users u
                            LEFT OUTER JOIN (SELECT user_id, id
                                            FROM portfolios) p
                                            ON u.id = p.user_id
                                            GROUP BY u.id) t1
                      LEFT OUTER JOIN (SELECT user_id
                                      FROM endorsements) e
                                      ON e.user_id = t1.id
                      GROUP BY t1.id, t1.username, t1.avatar_url, p_count ) t2
                LEFT OUTER JOIN (SELECT p.id, p.user_id
                                FROM portfolios p
                                INNER JOIN favorites
                                ON favorites.subject_id = p.id) fav_p
                ON fav_p.user_id = t2.id
                GROUP BY t2.id, t2.username, t2.avatar_url, p_count, ue_count) t3
          LEFT OUTER JOIN (SELECT p.id, p.user_id
                          FROM portfolios p
                          INNER JOIN endorsements
                          ON endorsements.portfolio_id = p.id) ep
          ON ep.user_id = t3.id
          GROUP BY t3.id, t3.username, t3.avatar_url, p_count, ue_count, fav_count) t4
    LEFT OUTER JOIN (SELECT p.id, p.user_id
                    FROM portfolios p
                    INNER JOIN views
                    ON views.subject_id = p.id) vp
    ON vp.user_id = t4.id
    GROUP BY t4.id, t4.username, t4.avatar_url, p_count, ue_count, fav_count, ep_count
    ORDER BY point DESC
    LIMIT 8

当我们点击标签本身时,我们实际调用函数... (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](); } } } (看一下渲染部分),它只使用onSelect

进行路由

为您的代码设置您的TouchableOpacity中的Action[tabbarbutton.props.name]()