Created
May 26, 2017 06:43
-
-
Save abdulmuneer22/41e3dd4b1284a158e9b73b79537b7fff to your computer and use it in GitHub Desktop.
Dynamic Tab View - React Native
This file contains hidden or 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, { 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> | |
); | |
} | |
} |
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
It shows an error "undefined is not an object (evaluating '_reactNative.View.propTypes.style')"