如何在react native中重用已渲染的本机组件?

时间:2022-11-09 19:42:27

In my application I have several screens and I use ReactNative.Navigator to navigate between them.
Following react native examples, my renderScene function is implemented in the following way:

在我的应用程序中,我有几个屏幕,我使用ReactNative.Navigator在它们之间导航。以下是本机示例的反应,我的renderScene函数以下列方式实现:

renderScene(route, navigator) {
    var routeId = route.id;
    switch (routeId)
    {
        case 'Screen1': return <Screen1 navigator={navigator} />
        case 'Screen2': return <Screen2 navigator={navigator} />
        case 'Screen3': return <Screen3 navigator={navigator} />
        ... 
    }
}

Taking this approach, it seems that every time I navigate to a specific screen, a native element is created all over again for this screen. It means that if I navigate to this screen several times, a new native element will be created for this screen hierarchy.
For example, if my navigation flow is Screen1 -> Screen2 -> Screen3 -> Screen2 then the native element that was already created for Screen2 in the first step, won't be reused for the last step.
Therefore, my question is, can I reuse the native element of a screen, or am I misunderstanding something?

采用这种方法,似乎每次我导航到特定屏幕时,都会为此屏幕重新创建本机元素。这意味着如果我多次导航到此屏幕,将为此屏幕层次结构创建一个新的本机元素。例如,如果我的导航流程是Screen1 - > Screen2 - > Screen3 - > Screen2,那么在第一步中为Screen2创建的本机元素将不会在最后一步中重复使用。因此,我的问题是,我可以重用屏幕的原生元素,还是我误解了什么?

1 个解决方案

#1


0  

In navigation you pushing numbers of screen class inside the navigation stack. So every-time you need to either push or pop screen from navigation stack. Now you question answer is either from it,

在导航中,您可以在导航堆栈中推送屏幕类的数量。因此,每次您需要从导航堆栈中推送或弹出屏幕。现在你质疑答案是从它,

1) You need to create new instance of screen2.

1)您需要创建screen2的新实例。

2) you can exchange index of screens inside navigation stack (exchange top n index i.e. screen 3 with index n-1 i.e. screen 2.

2)您可以在导航堆栈内交换屏幕索引(交换顶部n索引,即具有索引n-1的屏幕3,即屏幕2)。

3) Simply pop screen 3 you will screen 2 and use it according to your functionality.

3)只需弹出屏幕3,您将屏幕2并根据您的功能使用它。

#1


0  

In navigation you pushing numbers of screen class inside the navigation stack. So every-time you need to either push or pop screen from navigation stack. Now you question answer is either from it,

在导航中,您可以在导航堆栈中推送屏幕类的数量。因此,每次您需要从导航堆栈中推送或弹出屏幕。现在你质疑答案是从它,

1) You need to create new instance of screen2.

1)您需要创建screen2的新实例。

2) you can exchange index of screens inside navigation stack (exchange top n index i.e. screen 3 with index n-1 i.e. screen 2.

2)您可以在导航堆栈内交换屏幕索引(交换顶部n索引,即具有索引n-1的屏幕3,即屏幕2)。

3) Simply pop screen 3 you will screen 2 and use it according to your functionality.

3)只需弹出屏幕3,您将屏幕2并根据您的功能使用它。