简体   繁体   English

如何在 React Native 中只渲染一次组件

[英]How to render component only once in React Native

I am using Expo + React Navigation.我正在使用 Expo + React 导航。

There are many screens in my React Native apps which use a component Map .我的 React Native 应用程序中有许多使用组件Map屏幕。 I don't want this component to be re-rendered whenever switching screen.我不希望在切换屏幕时重新渲染此组件。 Are there any way to save my component Map to some kind of storage so that every time I need Map , my app doesn't have to re-render it?有什么方法可以将我的组件Map保存到某种存储中,以便每次我需要Map ,我的应用程序都不必重新渲染它?

If you want something should not re-render.如果你想要的东西不应该重新渲染。 You gave that in index page.你在索引页中给出了它。 If your give it in index page it can be access everywhere in project.如果您在索引页面中提供它,则可以在项目中的任何地方访问它。

Example:示例:

Whenever I use firebase I initialize firebase in index.js file.每当我使用 firebase 时,我都会在 index.js 文件中初始化 firebase。

Probably, you could play with shouldComponentUpdate() of you Map component.也许,您可以使用 Map 组件的shouldComponentUpdate()

Try to return true only for the first render and then return false for feature calls to tell React not to re-render your component.尝试仅在第一次渲染时返回true ,然后为功能调用返回false以告诉 React 不要重新渲染您的组件。

react-native works with virtual dom, when you switch to another screen, I think first screen is removed from the dom in which you have your map component, so it will be re rendered when you come back. react-native 与虚拟 dom 一起使用,当您切换到另一个屏幕时,我认为第一个屏幕会从您拥有地图组件的 dom 中删除,因此当您回来时它将重新呈现。 what you can do is you can render the map component at parent level where ur switching screens and pass it as children to your screen.您可以做的是,您可以在父级渲染地图组件,在那里您切换屏幕并将其作为子级传递到您的屏幕。 this is one solution but don't know whether its a best one这是一种解决方案,但不知道它是否是最好的解决方案

What if you cache your Map data on first render and then use cached data second time for faster rendering?如果您在第一次渲染时缓存地图数据,然后第二次使用缓存数据以加快渲染速度会怎样?

I would suggest you to go through the Map docs to look for any caching prop or just include any third party Map component that supports caching.我建议您浏览Map文档以查找任何缓存道具,或者只包含任何支持缓存的第三方Map组件。

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

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