React Native - Creating Navigator dynamically with React Navigation

后端 未结 3 831
不知归路
不知归路 2021-02-06 06:37

I am building a mobile application with React Native and I am using React Navigation to build a navigator inside my application. React navigation provided me a good way to handl

3条回答
  •  生来不讨喜
    2021-02-06 07:44

    Your TabStack file:

    const CATEGORIES = {
      "Food": { screen: FoodStack },
      // ...
    }
    
    export default (screenNames) => {
      const screens = screenNames.reduce((total, name) => ({...total, [name]: CATEGORIES[name]}), {})
      const TabStack = TabNavigator(screens,
        {
            tabBarComponent : props => ,
            tabBarPosition: 'top',
            animationEnabled : true,
            initialRouteName : 'Food',
            swipeEnabled: true,
            tabBarOptions : {
    
                scrollEnabled : true
            }
      })
      return TabStack
    }
    

    Your Root file:

    import getTabStack from './TabStack'
    
    
    class Root extends Component {
    
        state = {
          categoriesNames: null
        }
    
        componentWillMount() {
          // assuming result is ["Food", "Drink", ... ]
          Api.fetchCategories().then((result) => {
            this.setState({ categoriesNames: result })
          })
        }
    
        render() {
          const { categoriesNames } = this.state
          if (!categoriesNames) {
            return 
          }
          const TabStack = getTabStack(categoriesNames)
          return (
            
              
            
          );
        }
      }
    

提交回复
热议问题