gpt4 book ai didi

react-native - 我如何解决 React Native 中的这个 Hook 错误?

转载 作者:行者123 更新时间:2023-12-05 04:34:24 26 4
gpt4 key购买 nike

我正在尝试将这个应用程序作为一个项目,因为我刚刚学习了 React Native,并且我已经顺利完成了这一步,但我一直遇到这个 Hook 问题:

Uncaught Error: Invalid hook call. Hooks can only be called inside ofthe body of a function component. This could happen for one of thefollowing reasons:

  1. You might have mismatching versions of React and the renderer (such as React DOM)
  2. You might be breaking the Rules of Hooks
  3. 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 debugand 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:1at bootstrap:856:1

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",
},
});

最佳答案

错误很明显,您只能在函数组件的主体内部使用钩子(Hook)。

所有这些代码:

const [password, setPassword] = useState("");
const [username, setUsername] = useState("");
const [hasUser, setUser] = useState(false);
const [error, setError] = useState("");

应该移动到 App 组件中。就是这样

关于react-native - 我如何解决 React Native 中的这个 Hook 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71239776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com