I have implemented the react-native-tab-view
. I want to get the active tab or current tab object when we swipe left to right and vice-versa. I got a event onTabPress which used for get info the tab when we click on it. but there is no any event to get active tab info when we switch on it by swipe. How can we do that.
import * as React from 'react';
import {
Text, View, StyleSheet, Dimensions
} from 'react-native';
import { TabView, SceneMap, TabBar } from 'react-native-tab-view';
const FirstRoute = () => (
<View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);
const SecondRoute = () => (
<View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);
export default class SwipeTabs extends React.Component {
state = {
index: 0,
routes: [
{ key: 'first', title: 'Just News', id: 1 },
{ key: 'second', title: 'India', id: 2 },
{ key: 'first', title: 'World', id: 3 },
{ key: 'second', title: 'Startup', id: 4 },
{ key: 'first', title: 'Business', id: 5 },
{ key: 'second', title: 'Laws', id: 6 },
{ key: 'first', title: 'Politics', id: 7 },
{ key: 'second', title: 'Technology', id: 8 },
{ key: 'first', title: 'Personal Finance', id: 9 },
{ key: 'second', title: 'Sports', id: 10 },
],
};
render() {
return (
<TabView
style={{ marginTop: 0 }}
navigationState={this.state}
scrollEnabled
renderTabBar={(props) => (
<TabBar
onTabPress={(e) => console.log('onTabPress', e)}
{...props}
indicatorStyle={{ backgroundColor: 'white' }}
tabStyle={{ width: 150 }}
scrollEnabled
style={{ backgroundColor: 'blue' }}
/>
)}
renderScene={SceneMap({
first: FirstRoute,
second: SecondRoute,
})}
onIndexChange={(index) => {
console.log('index-index', index);
this.setState({ index });
}}
initialLayout={{ width: Dimensions.get('window').width }}
tabStyle={{ width: 'auto', backgroundColor: 'yellow' }}
/>
);
}
}
const styles = StyleSheet.create({
scene: {
flex: 1,
},
});