Created
August 1, 2018 01:07
-
-
Save mauriciopazpp/16a24268be750e4e62defc1313b8c15f to your computer and use it in GitHub Desktop.
Tabs for React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Button, Text, View } from 'react-native'; | |
import { Ionicons } from '@expo/vector-icons'; // Version can be specified in package.json | |
import { StackNavigator, TabNavigator, TabBarBottom } from 'react-navigation'; // Version can be specified in package.json | |
class HomeScreen extends React.Component { | |
render() { | |
return ( | |
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> | |
<Text>Home!</Text> | |
<Button | |
title="Go to Settings" | |
onPress={() => this.props.navigation.navigate('Settings')} | |
/> | |
<Button | |
title="Go to Details" | |
onPress={() => this.props.navigation.navigate('Details')} | |
/> | |
</View> | |
); | |
} | |
} | |
class SettingsScreen extends React.Component { | |
render() { | |
return ( | |
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> | |
<Text>Settings!</Text> | |
<Button | |
title="Go to Home" | |
onPress={() => this.props.navigation.navigate('Home')} | |
/> | |
<Button | |
title="Go to Details" | |
onPress={() => this.props.navigation.navigate('Details')} | |
/> | |
</View> | |
); | |
} | |
} | |
class DetailsScreen extends React.Component { | |
render() { | |
return ( | |
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> | |
<Text>Details!</Text> | |
</View> | |
); | |
} | |
} | |
const HomeStack = StackNavigator({ | |
Home: { screen: HomeScreen }, | |
Details: { screen: DetailsScreen }, | |
}); | |
const SettingsStack = StackNavigator({ | |
Settings: { screen: SettingsScreen }, | |
Details: { screen: DetailsScreen }, | |
}); | |
export default TabNavigator( | |
{ | |
Home: { screen: HomeStack }, | |
Settings: { screen: SettingsStack }, | |
}, | |
{ | |
navigationOptions: ({ navigation }) => ({ | |
tabBarIcon: ({ focused, tintColor }) => { | |
const { routeName } = navigation.state; | |
let iconName; | |
if (routeName === 'Home') { | |
iconName = `ios-information-circle${focused ? '' : '-outline'}`; | |
} else if (routeName === 'Settings') { | |
iconName = `ios-options${focused ? '' : '-outline'}`; | |
} | |
// You can return any component that you like here! We usually use an | |
// icon component from react-native-vector-icons | |
return <Ionicons name={iconName} size={25} color={tintColor} />; | |
}, | |
}), | |
tabBarComponent: TabBarBottom, | |
tabBarPosition: 'bottom', | |
tabBarOptions: { | |
activeTintColor: 'tomato', | |
inactiveTintColor: 'gray', | |
}, | |
animationEnabled: false, | |
swipeEnabled: false, | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment