I'm learning to use context with React native, actually i'm following a blog where define how use createContext and useContext for Authentication logic in any app with React and Navigation. In the blog not define how to import the class AuthService and use the context in another component, and i have an error because the functions and the state not exist when i try to import them in my Navigator page or my Login page.
The code:
AuthService.tsx
import React, {createContext, useState, useEffect} from 'react'
import AsyncStorage from '@react-native-community/async-storage'
const AuthContext = createContext({});
const AuthProvider = ({ children }) => {
const [auth, setAuthState] = useState({});
useEffect(() => {
GetLogedUser();
}, []);
const GetLogedUser=async()=> {
try {
let userData = await AsyncStorage.getItem("userloged"|| {});
let data = JSON.parse(userData);
return data;
} catch (error) {
console.log("Something went wrong", error);
setAuthState({});
return null;
}
}
const LoginUser=async(AuthData)=>{
try{
//many code omited...
if(response.status==200 && json.response === true && json.error===""){
let authUser = {id : json.data.id, nick: AuthData.nick, services: json.data.services };
await AsyncStorage.setItem("userloged", JSON.stringify(authUser));
setAuthState(authUser);
return "OK";
}
else{
return "Error response Login Service - "+ json.error;
}
}catch(err){
return "Error connecting or timeout service.";
}
}
return (
<AuthContext.Provider value={{ auth, LoginUser }}>
{children}
</AuthContext.Provider>
);
}
export { AuthContext, AuthProvider };
Navigator.tsx (How use the auth imported state?)
import React, {useContext} from 'react'
import { createStackNavigator } from '@react-navigation/stack'
import Login from '../Views/Login'
import Home from '../Views/Home'
import {AuthContext} from '../Services/Auth/AuthService'
const Stack = createStackNavigator();
const Navigator= (props) =>{
const { auth } = useContext(AuthContext);
return (
<Stack.Navigator>
{auth.id ? // <-- error undefined is not and object
(<>
<Stack.Screen name="Home" component={Home} options={{headerShown:false}}/>
</>)
: (<>
<Stack.Screen name="Login" component={Login} options={{headerShown:false}}/>
<Stack.Screen name="Register" component={Register} options={{title:'Register.'}}/>
</>)
}
</Stack.Navigator>
);
}
export default Navigator;
Login.tsx (How use the LoginUser imported function ???)
//..another imports omited
import {AuthContext} from '../Services/Auth/AuthService'
const Login=(props)=>{
const {LoginUser} = useContext(AuthContext); //<---how here?
const {navigation, users} = props;
const [userName, setUserName] =useState('');
const [password, setPassword] =useState('');
return(
<View style={styles.container}>
<View style={styles.middleContainer}>
<TextInput placeholder="Username..." defaultValue={userName} onChangeText={txt=> setUserName(txt)} style={styles.txtInputsLog}/>
<TextInput placeholder="Password..." defaultValue={password} onChangeText={txt=> setPassword(txt)} style={styles.txtInputsLog} secureTextEntry={true}/>
<Button primary style={styles.btnLogin} onPress={async ()=> await ValidateFields(userName, password) ? navigation.navigate('Home') : null}>
<Text style={styles.txtLogin}>Login</Text>
</Button>
</View>
</View>
);
async function ValidateFields(userName:string, password:string){
let result = await LoginUser({nick:userName, password: password}); //<--Error LoginUser is not a function
return result==="OK" ? true: false;
}
}
export default Login;