My react-native app allows people to use it without logging in, but some screens require the user to be logged in to access. I'm using react-navigation, and when an unauthenticated user navigates to one of these screens, I want to redirect to the log in screen.
Right now, the screen simply renders differently for a logged out user, but ideally it would not render at all (just redirect):
import { connect } from 'react-redux';
import ProfileScreen from './ProfileScreen';
const mapStateToProps = (state) => {
const { currentUser } = state;
return {
currentUser,
};
};
export default connect(mapStateToProps)(ProfileScreen);
// ProfileScreen.js
import React from 'react';
import { Text } from 'react-native';
const ProfileScreen = (props) => {
if (props.currentUser == null) {
return <Text>You are not logged in</Text>;
}
return <Text>Your Profile</Text>;
};
export default ProfileScreen;
Where is the proper place to put the redirect? I'm also using redux, so I've seen suggestions to put it in the componentDidMount of the container, or somewhere in the redux chain.