Good evening to y’all!!
I’m trying to click my “login or register button” but I’m receiving the following error: “TypeError: Cannot read property ‘navigate’ of undefined”
I tried to do small changes within the project but no joy 🙁
JavaScript
x
12
12
1
TypeError: Cannot read property 'navigate' of undefined
2
onPress
3
C:/Users/André Vieira/DoneWithit/app/screens/WelcomeScreen.js:21
4
18 | <View style={styles.buttonsContainer}>
5
19 | <Button
6
20 | title="Login"
7
> 21 | onPress={() => navigation.navigate(routes.LOGIN)}
8
| ^ 22 | />
9
23 | <Button
10
24 | title="Register"
11
View compiled
12
This is my login screen
JavaScript
1
82
82
1
import React, { useState } from "react";
2
import { StyleSheet, Image } from "react-native";
3
import * as Yup from "yup";
4
5
import Screen from "../components/Screen";
6
import {
7
ErrorMessage,
8
Form,
9
FormField,
10
SubmitButton,
11
} from "../components/forms";
12
import authApi from "../api/auth";
13
import useAuth from "../auth/useAuth";
14
15
const validationSchema = Yup.object().shape({
16
email: Yup.string().required().email().label("Email"),
17
password: Yup.string().required().min(4).label("Password"),
18
});
19
20
function LoginScreen(props) {
21
const auth = useAuth();
22
const [loginFailed, setLoginFailed] = useState(false);
23
24
const handleSubmit = async ({ email, password }) => {
25
const result = await authApi.login(email, password);
26
if (!result.ok) return setLoginFailed(true);
27
setLoginFailed(false);
28
auth.logIn(result.data);
29
};
30
31
return (
32
<Screen style={styles.container}>
33
<Image style={styles.logo} source={require("../assets/logo-red.png")} />
34
35
<Form
36
initialValues={{ email: "", password: "" }}
37
onSubmit={handleSubmit}
38
validationSchema={validationSchema}
39
>
40
<ErrorMessage
41
error="Invalid email and/or password."
42
visible={loginFailed}
43
/>
44
<FormField
45
autoCapitalize="none"
46
autoCorrect={false}
47
icon="email"
48
keyboardType="email-address"
49
name="email"
50
placeholder="Email"
51
textContentType="emailAddress"
52
/>
53
<FormField
54
autoCapitalize="none"
55
autoCorrect={false}
56
icon="lock"
57
name="password"
58
placeholder="Password"
59
secureTextEntry
60
textContentType="password"
61
/>
62
<SubmitButton title="Login" />
63
</Form>
64
</Screen>
65
);
66
}
67
68
const styles = StyleSheet.create({
69
container: {
70
padding: 10,
71
},
72
logo: {
73
width: 80,
74
height: 80,
75
alignSelf: "center",
76
marginTop: 50,
77
marginBottom: 20,
78
},
79
});
80
81
export default loginscreen;
82
Maybe it is something simple but I can’t figure it out.
Anyone, to help? Thank you
My Welcome Screen
JavaScript
1
60
60
1
import React from "react";
2
import { ImageBackground, StyleSheet, View, Image, Text } from "react-native";
3
4
import Button from "../components/Button";
5
import routes from "../navigation/routes";
6
7
function WelcomeScreen({ navigation }) {
8
return (
9
<ImageBackground
10
blurRadius={10}
11
style={styles.background}
12
source={require("../assets/background.jpg")}
13
>
14
<View style={styles.logoContainer}>
15
<Image style={styles.logo} source={require("../assets/logo-red.png")} />
16
<Text style={styles.tagline}>Sell What You Don't Need</Text>
17
</View>
18
<View style={styles.buttonsContainer}>
19
<Button
20
title="Login"
21
onPress={() => navigation.navigate(routes.LOGIN)}
22
/>
23
<Button
24
title="Register"
25
color="secondary"
26
onPress={() => navigation.navigate(routes.REGISTER)}
27
/>
28
</View>
29
</ImageBackground>
30
);
31
}
32
33
const styles = StyleSheet.create({
34
background: {
35
flex: 1,
36
justifyContent: "flex-end",
37
alignItems: "center",
38
},
39
buttonsContainer: {
40
padding: 20,
41
width: "100%",
42
},
43
logo: {
44
width: 100,
45
height: 100,
46
},
47
logoContainer: {
48
position: "absolute",
49
top: 70,
50
alignItems: "center",
51
},
52
tagline: {
53
fontSize: 25,
54
fontWeight: "600",
55
paddingVertical: 20,
56
},
57
});
58
59
export default WelcomeScreen;
60
From Console
JavaScript
1
26
26
1
Uncaught TypeError: Cannot read property 'navigate' of undefined
2
at onPress (WelcomeScreen.js:21)
3
at onClick (PressResponder.js:333)
4
at HTMLUnknownElement.callCallback (react-dom.development.js:188)
5
at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
6
at invokeGuardedCallback (react-dom.development.js:292)
7
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:306)
8
at executeDispatch (react-dom.development.js:389)
9
at executeDispatchesInOrder (react-dom.development.js:414)
10
at executeDispatchesAndRelease (react-dom.development.js:3278)
11
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:3287)
12
at forEachAccumulated (react-dom.development.js:3259)
13
at runEventsInBatch (react-dom.development.js:3304)
14
at runExtractedPluginEventsInBatch (react-dom.development.js:3514)
15
at handleTopLevel (react-dom.development.js:3558)
16
at batchedEventUpdates$1 (react-dom.development.js:21871)
17
at batchedEventUpdates (react-dom.development.js:795)
18
at dispatchEventForLegacyPluginEventSystem (react-dom.development.js:3568)
19
at attemptToDispatchEvent (react-dom.development.js:4267)
20
at dispatchEvent (react-dom.development.js:4189)
21
at unstable_runWithPriority (scheduler.development.js:653)
22
at runWithPriority$1 (react-dom.development.js:11039)
23
at discreteUpdates$1 (react-dom.development.js:21887)
24
at discreteUpdates (react-dom.development.js:806)
25
at dispatchDiscreteEvent (react-dom.development.js:4168)
26
Advertisement
Answer
It seems like WelcomeScreen isn’t inside of the navigator the passes down navigation as props. If you’re using react-navigation 5.x you can use the hook:
JavaScript
1
63
63
1
import React from "react";
2
import { ImageBackground, StyleSheet, View, Image, Text } from "react-native";
3
import { useNavigation } from '@react-navigation/native';
4
5
import Button from "../components/Button";
6
import routes from "../navigation/routes";
7
8
function WelcomeScreen() {
9
const navigation = useNavigation();
10
11
return (
12
<ImageBackground
13
blurRadius={10}
14
style={styles.background}
15
source={require("../assets/background.jpg")}
16
>
17
<View style={styles.logoContainer}>
18
<Image style={styles.logo} source={require("../assets/logo-red.png")} />
19
<Text style={styles.tagline}>Sell What You Don't Need</Text>
20
</View>
21
<View style={styles.buttonsContainer}>
22
<Button
23
title="Login"
24
onPress={() => navigation.navigate(routes.LOGIN)}
25
/>
26
<Button
27
title="Register"
28
color="secondary"
29
onPress={() => navigation.navigate(routes.REGISTER)}
30
/>
31
</View>
32
</ImageBackground>
33
);
34
}
35
36
const styles = StyleSheet.create({
37
background: {
38
flex: 1,
39
justifyContent: "flex-end",
40
alignItems: "center",
41
},
42
buttonsContainer: {
43
padding: 20,
44
width: "100%",
45
},
46
logo: {
47
width: 100,
48
height: 100,
49
},
50
logoContainer: {
51
position: "absolute",
52
top: 70,
53
alignItems: "center",
54
},
55
tagline: {
56
fontSize: 25,
57
fontWeight: "600",
58
paddingVertical: 20,
59
},
60
});
61
62
export default WelcomeScreen;
63