aksonov / react-native-router-flux

The first declarative React Native router
MIT License
8.99k stars 2.11k forks source link

How to create Tabbar with icon&Text #2446

Closed hapo-nghialuu closed 7 years ago

hapo-nghialuu commented 7 years ago

I search in doc, i don't know how to create tabbar with icon&Text use react-native-router-flux. Please help me!!!! Thank you very much!!!!

Version

Tell us which versions you are using:

ghoshabhi commented 7 years ago

I also want to look at a clean example of just tabbed navigation.

mcabs3 commented 7 years ago

this can be done using icon and tabBarLabel component on an individual Tabbed Scene.

<Tabs>
  <Scene
  tabBarLabel={'Something'}
  icon={IconComponent}
  />
</Tabs>
hapo-nghialuu commented 7 years ago

thank you very much!

dwilt commented 7 years ago

Just a heads up. This seems really weird, and might be a bug, but you can't pass a rendered component to the icon prop:

<Scene
    hideNavBar
    key={MY_ACCOUNT_KEY}
    tabBarLabel={`My Account`}
    component={MyAccount}
    icon={<Icon
            size={iconSize}
            color={iconColor}
            name={`home3`}
        />}
/>

You get this error: image

The way around it is to wrap the component in a function:

<Scene
    hideNavBar
    key={MY_ACCOUNT_KEY}
    tabBarLabel={`My Account`}
    component={MyAccount}
    icon={() => (
        <Icon
            size={iconSize}
            color={iconColor}
            name={`home3`}
        />
    )}
/>
dwilt commented 7 years ago

It seems the icon prop uses the v3 callback as explained here. Except now, the selected prop is now focused. Here's how I got it working:

<Tabs
    showLabel={false}
    lazy={true}
    tabStyle={styles.tab}
    tabBarStyle={styles.tabs}
    labelStyle={styles.label}
    swipeEnabled={false}
>
    <Scene
        hideNavBar
        key={MY_WATER_KEY}
        component={MyWater}
        icon={({ focused }) => (
            <Icon
                size={iconSize}
                color={focused ? activeIconColor : iconColor}
                name={`drop2`}
                text={`My Water`}
                textStyle={focused ? [styles.label, styles.activeLabel] : styles.label}
            />
        )}
    />
    <Scene
        hideNavBar
        key={INBOX_KEY}
        component={Inbox}
        icon={({ focused }) => (
            <Icon
                size={iconSize}
                color={focused ? activeIconColor : iconColor}
                textStyle={focused ? [styles.label, styles.activeLabel] : styles.label}
                name={`envelope`}
                text={`Messages`}
            />
        )}
    />
    <Scene
        hideNavBar
        key={MY_ACCOUNT_KEY}
        component={MyAccount}
        icon={({ focused }) => (
            <Icon
                size={iconSize}
                color={focused ? activeIconColor : iconColor}
                textStyle={focused ? [styles.label, styles.activeLabel] : styles.label}
                name={`home3`}
                text={`My Account`}
            />
        )}
    />
</Tabs>

image

By the way, I wanted more control over the labels so I passed the text into my Icon component and render it there and use the showLabel={false} on the Tabs component.

ghoshabhi commented 7 years ago

@dwilt Thanks for the example. Just had a small question. How do I change the scene being rendered based on the tab click ? Just usual Action.sceneKey() ?

Thanks.

Sorry forgot we had access to the component prop! 🤕 Thanks again!

dwilt commented 7 years ago

@ghoshabhi yep

wincod75 commented 6 years ago

Does this still work? My text label is not showing...

icon={({ focused }) => ( <Icon size={iconSize} color={focused ? activeIconColor : iconColor} textStyle={focused ? [styles.label, styles.activeLabel] : styles.label} name={home3} text={My Account} /> )}

uncvrd commented 6 years ago

@wincod75 any luck after a day? I'm just running into this issue as well and cannot get anything to display

wincod75 commented 6 years ago

@uncvrd No luck, can't get anything to display either.

uncvrd commented 6 years ago

@wincod75 ugh well keep me posted if you do. I'll keep searching

webjyh commented 6 years ago

@uncvrd @wincod75

const TabIcon = (props) => {
    console.log({...props});
    return (
        <View>
            <Text>{JSON.stringify(props.focused)}</Text>
        </View>
    )
};

<Scene icon={TabIcon} ... />