grahammendick / navigation

Scene-Based Navigation for React and React Native
https://grahammendick.github.io/navigation/
Apache License 2.0
571 stars 40 forks source link

Prevented FOUC on tab change after theme change #785

Closed grahammendick closed 2 months ago

grahammendick commented 3 months ago

Fixes #784

Inactive tabs are suspended and don't rerender when the theme changes from light to dark, for example. Because the tab change happens on the native side, the light version of the tab will show until the dark version renders when the tab is unsuspended. This can result in a flash of unstyled content (FOUC).

Added a preventFouc prop. Set this to true on theme change and set it back to false when the tab changes. Then, whenever any inactive tab is first pressed, the TabBar triggers the change on the JavaScript side instead of the native side.

const [preventFouc, setPreventFouc] = useState(false);

<Button onPress={() => setPreventFouc(true)>Change Theme</Button> 

<TabBar preventFouc={preventFouc} onChangeTab={() => setPreventFouc(false)}>