I have a React Native screen with ScrollView displaying countries with their phone country codes.
const CountryCodes = () => {
import countries from "../../utils/countries";
return (
<ScrollView>
{countries.map((country) => {
return renderItem(country);
})}
</ScrollView>
);
};
the countries.js file is just an array of country data (c. 200 objects), together with require statements for the related assets, like this:
const countries = [{
letterCode: "ad",
phoneCode: "376",
name: "Andorra",
flag: require("../assets/flags/ad.png"),
}]
I am using the Stack Navigator from React Navigation. The problem that I have is that when I am navigating to the CountryCodes screen, the screen is freezing (while, I assume, all the static flag images are loading) and after ~1 second it properly displays the ScrollView with all the countries.
Is there a way to make the transition smoother? I tried displaying a loader while the countries are loading, but the screen just keeps freezing with no chance to display anything in the meantime.
This issue is the same when using FlatList.
Please see the GIF below for reference.
3
Answers
This lead me to a lot of frustration, but I managed to work out a sort of a hack to accomplish the goal with the use of setTimeout, like this:
When wrapped like this, the setLoading is executed first (and a lading screen appears) and only after after that the navigation.navigate is called and the Select Country screen appears after all the data is loaded.
React native
ScrollView
doesn’t do very well with a large number of entries. The recommendation is to use aFlatlist
as it only renders the components that are currently visible in the viewport.You can see the usage here. If you need even faster performance, there’s FlashList by shopify which is blazing fast.Example:
You might need to change your
renderItem
to match the FlatList’s renderItem signature.This is an issue while loading a heavy component. this happen to me while loading a country list , multiple graph and other heavy components.
if you press on a navigation button then there is some delay while navigating to that screen or component. I have write a custom hook for this.
paste the following code in your useIsReady.js
here you can display a loading indicator on that component or screen Like this.
}
or you can use lazy loading for multiple heavy components.
Here is the example for lazy loading