简体   繁体   English

React Navigation Drawer:动态路由和导航项目?

[英]React Navigation Drawer: Dynamically routes and navigation items?

Right now I am facing a problem.. Before my application gets mounted, I am fetching categories from an API. 现在,我面临一个问题。在安装应用程序之前,我正在从API获取类别。 Now I want to have a dynamic app which always contains the newest data. 现在,我想要一个动态应用程序,其中始终包含最新数据。

I want to have the different categories always as a separated screen. 我希望始终将不同的类别作为单独的屏幕显示。 The functions I need for every category screen is always the same. 每个类别屏幕所需的功能始终相同。 So, I have build myself a screen template. 因此,我为自己构建了一个屏幕模板。

However, I have to fetch the different categories from the API and then import my template screen as many times as I have fetched categories to create a screen for each category. 但是,我必须从API获取不同的类别,然后将模板屏幕导入的次数与获取类别的次数相同,才能为每个类别创建一个屏幕。 Furthermore, I need to pass the category to the template screen to set up a correct screen for each category.. 此外,我需要将类别传递到模板屏幕以为每个类别设置正确的屏幕。

Is this possible and can you give me a hint on how to do this? 这有可能吗,您能给我一个提示吗?

Kind regards and Thank You! 亲切的问候,谢谢!

I understand that you want your drawer navigation to be dynamic in relation to the data received from the API. 我了解您希望您的抽屉导航相对于从API接收的数据而言是动态的。 In that case, you should only create and render the drawer navigation after the api call . 在这种情况下,您在api调用之后创建和呈现抽屉导航

After fetching the categories, iterate through them and build your route config. 提取类别后,遍历它们并构建您的路由配置。 You should be able to override the navigationOptions and manually set some params (containing your specific category) for each screen. 您应该能够覆盖navigationOptions并为每个屏幕手动设置一些参数(包含您的特定类别)。

let routeConfig = {};

categories.map(category => {
    routeConfig[category.key] = {
        screen: CategoryScreen, // your template screen, common to every item
        navigationOptions: (props) => {
            props.navigation.setParams({ category });
        }
    }
});

const DrawerNav = createDrawerNavigation(routeConfig, { /*options*/ });

Then you'll have to render the drawer navigation component manually 然后,您必须手动呈现抽屉导航组件

render() {
    return (
        <DrawerNav screenProps={{}} />
    );
}

Finally, within CategoryScreen or whatever your screen component is, you can access the category in a way similar to the clasic navigation case: this.props.navigation.state.params.category . 最后,在CategoryScreen或屏幕组件中的任何一个中,您都可以以类似于this.props.navigation.state.params.category导航方式的方式访问类别: this.props.navigation.state.params.category

Some notes: 一些注意事项:

  • The DrawerNav constant is just an example. DrawerNav常数只是一个示例。 Most probably you're going to want to make it a class property or a normal variable defined outside of the class but populated within. 最有可能您将要使其成为类属性或在类外部定义但填充在其中的普通变量。
  • category.key is yet again an example, you should make sure to replace it with something unique for each category, such as a string representation of the category id. category.key还是一个示例,您应该确保将其替换为每个类别唯一的内容,例如类别ID的字符串表示形式。
  • If you want to have different screen components for each category, you can simply pass a different component to screen based on the category in the current .map() iteration. 如果要为每个类别使用不同的屏幕组件,则可以简单地根据当前.map()迭代中的类别将不同的组件传递到screen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM