const AuthContext = React.createContext<any>({ currentUser: null, signUp: null});
export function useAuth() {
return useContext(AuthContext);
}
export function AuthProvider({ children }) {
const [ currentUser, setCurrentUser ] = useState<any>();
function signUp(email?: string, password?: string){
if (email && password && email.length > 3 && password.length > 5){
return auth.createUserWithEmailAndPassword(email, password);
}
return auth.signInWithPopup(googleAuthProvider);
}
useEffect(() => {
const unsubscriber = auth.onAuthStateChanged( user => {
setCurrentUser(user);
})
return unsubscriber;
}, [])
const value = {
currentUser,
signUp,
}
return (
<AuthContext.Provider value={value}>
{children}
</AuthContext.Provider>
)
}
This is how i imported, deconstructed and used
import {useAuth} from "./{some-path}/AuthContext";
function SignUp() {
const { signUp } = useAuth();
function insideSomeFunction(){
signUp!();
}
return (
{simple react-nataive componenst to call insidesomefunction}
)
}
Problem is whenever I try to use signUp by importing useAuth and deconstructing it to get signUp function, it always give null, even tho I have provided the implementation in the AuthProvider function.
- I am new to react-native and using context in react