Splash screen & loading data ๐พ
In many applications, you need to load data from API before displaying any content. To do that, we built a solid navigation based on a splash screen to load data before the content shows, and inline require to improve performance.
How the navigation is build โ
The answer is :
Like it's recommended in the React Navigation V5 documentation ๐ค
Like everywhere else, the entry point of the navigation is in the root file :
What is new here is into the ApplicationNavigator
component :
So the root navigator is a stack with two screens :
- the splash screen (
IndexStartupContainer
), - a second navigator (
MainNavigator
).
The main goal of the ApplicationNavigator
is to only have one screen (the IndexStartupContainer
) to load.
And, when the application finish loading, then fetch and display the MainNavigator
.
In other words, when ApplicationNavigator
is mounted, it only can display the IndexStartupContainer
because the MainNavigator
isn't loaded and imported yet.
In the StartupContainer
, the redux action which is used to load data on init application is trigger and when the action is finish, the state state.startup.initialize.loading
turns true
.
when this state is true, in the useEffect the MainNavigator
navigator is imported , the navigation navigate and reset to a screen of the MainNavigator
.
To conclude, all new screens have to be added to MainNavigator
. The ApplicationNavigator
increase startup performance thanks to inline require and provides a splash screen to load your data.
How to load data before app open โ
To have a great separation of concerns, all API call are make into Services. In the above section, it said that in IndexStartupContainer
, a redux action is triggered. This action is InitializeStartupAction
:
In redux, triggering an action lead to an associated reducer and in most cases the action pass trough a middleware.
All the logic can be found at Stores/Startup/Init.js
.
All stores are based on redux-toolkit to simplify the process of API calls by using the createAsyncThunk
function (hidden by the buildAction
action which is a store builder function).
So, to load data on splash screen you just have to add dispatched action in the buildAction like FetchOne
and DefaultTheme
in the above example.