I am trying to load settings with AsyncStorage and set some props to next screen. I use sortBy
value to sort the Database results.
If I pass props with screenProps
from App and it causes more renders because it takes time for sortBy
value to load. Since AuthLoadingScreen
decides what to render, I want to decrease the amount of renders by passing props directly to MainList
AuthLoadingScreen.js
export default class AuthLoadingScreen extends Component {
constructor(props) {
super(props);
this._bootstrapAsync();
}
// Fetch the token from storage then navigate to our appropriate place
_bootstrapAsync = async () => {
const userToken = await AsyncStorage.getItem('token');
const sortValue = await AsyncStorage.getItem('sortBy');
if (userToken) {
this.props.navigation.navigate('App',{"sortBy":sortValue}); // I can't send this back to MainList of AppStack
} else {
this.props.navigation.navigate('Onboarding');
}
};
render() {
return (
<View>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
App.js
export default class App extends Component<Props> {
render() {
return (
<Root>
<AppContainer/>
</Root>
);
}
}
const AppStack = createStackNavigator(
{
MainList: {
screen: MainListScreen // how to pass props from AppStack to MainListScreen ?
},
Settings: {
screen: Settings,
}
},
{
initialRouteName:"MainList",
defaultNavigationOptions: () => ({
headerStyle: {
backgroundColor: '#2196f3'
},
headerTintColor: 'white'
})
}
);
const AppNavigator = createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
Onboarding:Onboarding,
App: AppStack, //logged in users
},
{
initialRouteName: 'AuthLoading',
}
);
const AppContainer = createAppContainer(AppNavigator);