使用导航器推送新路线时出现问题 [英] Issue when pushing new route with Navigator

查看:44
本文介绍了使用导航器推送新路线时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Navigator 时出现奇怪的行为.我第一次调用 navigator.push(...) 时,有时会打印以下警告:

I'm having a strange behavior with Navigator. The first time I will call navigator.push(...), it will sometimes print the following warning:

Warning: flattenChildren(...): two children with the same key, 'scene_1'. Child keys must be unique; when two children share a key, only the first child will be used.
  in RTCView (created by View)
  in View (created by Navigator)

或者,它会显示一个空白页.

Or, it will display a blank page.

但是,如果我再次弹出并推送,它会起作用......发生了什么?

However, if I pop and push again, it will work... What's happening?

这是代码:

import Page1 from '../page/Page1';
import Page2 from '../page/Page2';
import Page3 from '../page/Page3';

const routes = [
    { index: 0, title: 'Page 1' , component: Page1 },
    { index: 1, title: 'Page 2', component: Page2 },
    { index: 2, title: 'Page 3', component: Page3 },
];

class MyApp extends Component {

    renderScene(route, navigator) {

        let Component = route.component ;

        if (route.component) {
            return (
                <Component navigator={ navigator } />
            );
        }
        return null;
    }

    render() {

        return (
            <Navigator
                initialRoute={ routes[0] }
                initialRouteStack={ routes }
                renderScene={ this.renderScene } 
            />
        );
    }
}

=> 我刚刚尝试从导航器中删除 initialRouteStack 道具,它似乎解决了我的问题.但是我不明白这个道具是用来做什么的...

=> I've just tried to remove the initialRouteStack prop from the Navigator and it seems to have solved my issue. I however do not understand what is this prop used for...

推荐答案

initialRouteStack 是您为导航器提供的堆栈.通常你只传递一个初始路由,所以你的堆栈将从该路由开始.

initialRouteStack is the stack you're giving the navigator to begin with. Normally you just pass an initialRoute, so you're stack will start with just that route.

但是您将所有路由传递到 initialRouteStack 中,因此当您导航到另一个场景时,您基本上是将组件推送到已经包含该组件的堆栈的末尾.希望这是有道理的,并澄清事情.

But you were passing all the routes into the initialRouteStack, so when you navigated to another scene you were basically pushing components to the end of that stack which already contains that component. Hope this makes sense and clarifies things.

这篇关于使用导航器推送新路线时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆