Guest User

Untitled

a guest
Oct 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import React from 'react';
  2. import { TabView, TabBar } from 'react-native-tab-view';
  3. import { StyleSheet, View } from 'react-native';
  4.  
  5. import Ask from './home/Ask';
  6. import New from './home/New';
  7. import Show from './home/Show';
  8. import Top from './home/Top';
  9.  
  10. const styles = StyleSheet.create({
  11. tabbar: {
  12. backgroundColor: 'white'
  13. },
  14. labelStyle: {
  15. color: '#ff6600'
  16. },
  17. indicatorStyle: {
  18. height: StyleSheet.hairlineWidth * 10,
  19. backgroundColor: '#ff6600'
  20. }
  21. });
  22.  
  23. const Tabs = {
  24. top: () => <Top />,
  25. new: () => <New />,
  26. ask: () => <Ask />,
  27. show: () => <Show />
  28. };
  29.  
  30. class Home extends React.PureComponent {
  31. state = {
  32. index: 0,
  33. routes: [
  34. { key: 'top', title: 'Top' },
  35. { key: 'new', title: 'New' },
  36. { key: 'ask', title: 'Ask' },
  37. { key: 'show', title: 'Show' }
  38. ]
  39. };
  40.  
  41. _routesRendered = {};
  42.  
  43. _renderScene = ({ route }) => {
  44. // lazy load route
  45. const { index, routes } = this.state;
  46. const currRouteIdx = routes.indexOf(route);
  47. if (
  48. Tabs[route.key] &&
  49. (this._routesRendered[route.key] || index === currRouteIdx)
  50. ) {
  51. // check if we rendered
  52. this._routesRendered[route.key] = true;
  53. return Tabs[route.key](); // use factory function (performance)
  54. }
  55. return <View />;
  56. };
  57.  
  58. _onIndexChange = index => {
  59. this.setState({ index });
  60. };
  61.  
  62. _renderTabBar = props => (
  63. <TabBar
  64. {...props}
  65. style={styles.tabbar}
  66. pressColor="#d6d6d6"
  67. labelStyle={styles.labelStyle}
  68. indicatorStyle={styles.indicatorStyle}
  69. useNativeDriver
  70. />
  71. );
  72.  
  73. render() {
  74. return (
  75. <TabView
  76. navigationState={this.state}
  77. renderScene={this._renderScene}
  78. renderTabBar={this._renderTabBar}
  79. onIndexChange={this._onIndexChange}
  80. tabBarPosition="bottom"
  81. />
  82. );
  83. }
  84. }
Add Comment
Please, Sign In to add comment