我如何解决 React Native 中的这个 Hook 错误?
2022-02-23
559
我尝试将此应用程序作为一个项目,因为我刚刚学习了 React Native,到目前为止我没有遇到任何问题,但我一直遇到这个 Hook 问题:
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",
},
});
2个回答
错误很明显,您只能在函数组件的主体内使用钩子。
因此,所有这些代码:
const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [hasUser, setUser] = useState(false);
const [error, setError] = useState("");
应移至
App
组件内。就是这样
Giovanni Esposito
2022-02-23
所有
useState
调用都需要在 React 组件内,在你的情况下可以是
AppNavigator
:
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("");
///....
};
请参阅 Hooks 规则 以了解更多信息。
Youssouf Oumar
2022-02-23