I am using a Modal
in my RN app to show a map to the user. This component is slow and expensive to load, which is why I want to make sure it is not getting loaded from scratch every time.
However, currently this is not working. By toggling visible
, the map component gets loaded from scratch each and every time the modal opens up again.
Is there a way to prevent this behavior and make sure that the map is not getting unmounted and reloaded every time?
import { Modal } from 'react-native';
<Modal
animationType="slide"
transparent={true}
visible={showModal}
onRequestClose={() => setShowModal(false)}>
<MapView onTogglePressed={() => setShowModal(false)} />
</Modal>
Note: I do not have to use the
Modal
. Alternative components would be acceptable too.
2
Answers
Using this code, you can avoid unmounting and reloading the MapView component every time the modal opens up again.
I do have a couple of suggestions that you can work with:
Wrapping with
memo
can prevent re-rendering for functional components.Then use it in your Modal like:
key
prop:I hope this helps!