Reputation: 23
Im Trying to make this app as a Project because i just learned React Native and i've gotten this far without problems but i keep getting this Hook problem:
Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- You might have mismatching versions of React and the renderer (such as React DOM)
- You might be breaking the Rules of Hooks
- You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem. at resolveDispatcher (react.development.js:1476:1) at useState (react.development.js:1507:1) at Module../App.js (App.js:11:1) at webpack_require (bootstrap:789:1) at fn (bootstrap:100:1) at Module../node_modules/expo/AppEntry.js (AppEntry.js:1:1) at webpack_require (bootstrap:789:1) at fn (bootstrap:100:1) at Object.1 (index.js:18:1) at webpack_require (bootstrap:789:1) at bootstrap:856:1 at bootstrap:856:1
React Native:
import React, { useState } from "react";
import { StyleSheet, Text, View, TextInput, Button } from "react-native";
import { createStackNavigator } from "@react-navigation/stack";
import { NavigationContainer } from "@react-navigation/native";
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();
const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [hasUser, setUser] = useState(false);
const [error, setError] = useState("");
export const AppNavigator = () => {
const check = hasUser;
if (check == true) {
return (
<Tab.Navigator>
<Tab.Screen name="Feed" component={FeedScreen} />
</Tab.Navigator>
);
}
if (check == false) {
return (
<Stack.Navigator>
<Stack.Screen name="Login" component={LoginScreen} />
</Stack.Navigator>
);
}
};
const LoginScreen = () => {
<View style={styles.container}>
<Text style={styles.titleLogin}>VKAW</Text>
<View style={styles.inputs}>
<Text style={{ color: "red" }}>{error}</Text>
<TextInput
placeholder="Benutzername"
onChangeText={(usernameSet) => setUsername(usernameSet)}
value={username}
></TextInput>
<TextInput
placeholder="Password"
secureTextEntry={true}
onChangeText={(passwordSet) => setPassword(passwordSet)}
value={password}
></TextInput>
</View>
<Button title="Login" color="#ff8800" onPress={login()} />
<StatusBar style="auto" />
</View>;
};
const FeedScreen = () => {
<View>
<Text>Feed</Text>
</View>;
};
const login = () => {
const usernameCheck = username;
const passwordCheck = password;
if (usernameCheck == "test" && passwordCheck == "test") {
setUser(true);
} else {
setError("Passwort oder Benutzername Falsch!");
}
};
const App = () => {
return (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);
};
export default App();
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
titleLogin: {
fontSize: 30,
fontWeight: "bold",
color: "#ff8800",
paddingBottom: 50,
},
inputs: {
display: "flex",
width: 150,
paddingBottom: 50,
justifyContent: "center",
alignItems: "center",
},
});
Upvotes: 2
Views: 723
Reputation: 46291
All the useState
calls need to be inside a React component, which can be AppNavigator
in your case:
const Stack = createStackNavigator();
const Tab = createBottomTabNavigator();
export const AppNavigator = () => {
const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [hasUser, setUser] = useState(false);
const [error, setError] = useState("");
///....
};
See Rules of Hooks to learn more.
Upvotes: 0
Reputation: 11176
The error is clear, you can use hooks only inside of the body of a function component.
So all this code:
const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [hasUser, setUser] = useState(false);
const [error, setError] = useState("");
should be moved inside App
component. That's it
Upvotes: 2