Estoy usando Firebase para realizar un seguimiento de todos los usuarios. Pude conectar Firebase. El registro de usuario funciona bien.
Tengo problemas para registrar a los usuarios. Cuando ingreso el nombre de usuario y la contraseña, la aplicación no me redirige a la pantalla correcta y también muestra esta advertencia [Unhandled promise rejection: TypeError: navigation.navigation is not a function. (In 'navigation.navigation("myProfile")', 'navigation.navigation' is undefined)]
Así es como lo hago
import { useNavigation } from "@react-navigation/native"; import React, { useEffect, useState } from "react"; import { View, ...} from "react-native"; import { auth } from "../firebase"; const Profile = () => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const navigation = useNavigation(); useEffect(() => { const unsubscribe = auth.onAuthStateChanged((user) => { if (user) { navigation.navigation("myProfile"); } }); return unsubscribe; }, []); const signIn = () => { auth .signInWithEmailAndPassword(email, password) .then((userCredentials) => { const user = userCredentials.user; console.log("Sign In user.email = " + user.email); }) .catch((error) => alert(error.message)); }; //more codeTambién probé esto, pero no ayudó
import React, { useEffect, useState } from "react"; import { View, ...} from "react-native"; import { auth } from "../firebase"; const Profile = ({navigation}) => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); useEffect(() => { const unsubscribe = auth.onAuthStateChanged((user) => { if (user) { navigation.navigation("myProfile"); } }); return unsubscribe; }, []); //more code No estoy seguro si necesito navigation.navigation("myProfile") onPress o si tiene que estar dentro del useEffect
Usar
navigation.navigate("myProfile")en vez de
navigation.navigation("myProfile");También puedes destruir tu objeto de gancho de esta manera
const { navigate } = useNavigation()solo para escribir
navigate("myProfile")No estoy seguro de si necesito navegación. navegación ("mi perfil") onPress o si tiene que estar dentro de useEffect
En la mayoría de los casos, debe mover al usuario a la pantalla de inicio (pantalla de usuario conectado) cuando la función de inicio de sesión devuelve verdadero y el usuario ha iniciado sesión, por lo que en este caso creo que debe usar navegar ("mi perfil") dentro de
auth .signInWithEmailAndPassword(email, password) .then((userCredentials) => { ...your code navigation.navigate("myProfile") })