I'm making a small app in react native, it's a basic login workflow that use a couple of screen (login and home), that are shown based on user state; in App.js i have a bunch of state that im using to enable/disbale views, buttons, show username and so on. I have both the login screen and the home screen in external components. i'm wonder (besides using context) if there is a way to have these states (and functions) available to child screen
this is part of my code:
App.js
export default function App() {
const [isLoading, setIsLoading] = useState(true);
const [isLogged, setIsLogged] = useState(false);
const [userToken, setUserToken] = useState(null);
const [userProfile, setUserProfile] = useState(null);
const [email, setEmail] = useState(null);
const [password, setPassword] = useState(null);
const [loggingIn, setloggingIn] = useState(false);
const [error, setError] = useState(null);
const AppStack = createStackNavigator();
useEffect(() => {
AsyncStorage.getItem('userProfile').then((value) => {
if (value) {
console.log(JSON.parse(value)),
setUserProfile(JSON.parse(value)),
setIsLoading(false),
setIsLogged(true);
} else {
setIsLoading(false), setIsLogged(false);
}
});
}, []);
const doLogout = async () => {
---- LOGOUT CODE
}
const doLogin = async () => {
----- LOGIN CODE and UPDATE of state and asyncstorage
}
return (
<NavigationContainer>
<AppStack.Navigator initialRouteName="Login">
<AppStack.Screen name="Login" component={Login} />
<AppStack.Screen
name="Home"
component={Home}
options={{ headerShown: false }}
/>
</AppStack.Navigator>
</NavigationContainer>
);
Login.js
const Login = ({ loggingIn, userProfile }) => {
console.log(userProfile);
return (
<View style={styles.container}>
<Button
title="Login to Site"
onPress={() => doLogin()}
disabled={loggingIn}
>
{userProfile && userProfile.name} Login to Site
</Button>
</View>
);
};
how can access loggingIn (or even userProfile) and doLogin() function that i set and create (and update) in App.js? there should be an easy way (beside useContext) for simple uses like this one.