Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abdulmuneer22/41e3dd4b1284a158e9b73b79537b7fff to your computer and use it in GitHub Desktop.
Save abdulmuneer22/41e3dd4b1284a158e9b73b79537b7fff to your computer and use it in GitHub Desktop.
Dynamic Tab View - React Native
import React, { Component } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { TabViewAnimated, TabBar } from 'react-native-tab-view';
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20,
},
tabbar: {
backgroundColor: '#222',
},
page: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
indicator: {
backgroundColor: '#ffeb3b',
},
label: {
color: '#fff',
fontWeight: '400',
},
});
export default class DynamicExample extends Component {
static title = 'Dynamic tab load';
static appbarElevation = 0;
static propTypes = {
style: View.propTypes.style,
};
state = {
index: 0,
routes: [],
loading: true,
data: {},
};
componentDidMount() {
fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
const routes = [];
responseJson.movies.forEach((movie) => {
routes.push({
title: movie.title,
key: movie.releaseYear,
});
});
this.setState({
data: responseJson,
routes,
loading: false,
});
});
}
_handleChangeTab = (index) => {
this.setState({ index });
};
_renderHeader = (props) => {
return (
<TabBar
{...props}
scrollEnabled
indicatorStyle={styles.indicator}
style={styles.tabbar}
labelStyle={styles.label}
/>
);
};
_renderScene = ({ route }) => {
return (
<View style={[ styles.page, { backgroundColor: '#673ab7' } ]}>
<Text>{route.key} - {route.title}</Text>
</View>
);
};
renderScreen () {
if (this.state.loading) {
return (
<View>
<Text>Loading...</Text>
</View>
);
} else {
return (
<TabViewAnimated
style={styles.container}
navigationState={this.state}
renderScene={this._renderScene}
renderHeader={this._renderHeader}
onRequestChangeTab={this._handleChangeTab}
/>
);
}
}
render() {
return (
<View style={styles.container}>
{this.renderScreen()}
</View>
);
}
}
@ashu777
Copy link

ashu777 commented Jun 4, 2019

It shows an error "undefined is not an object (evaluating '_reactNative.View.propTypes.style')"

@abdulmuneer22
Copy link
Author

Must be because of outdated dependancy , please be aware that this script was written 2 years back :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment