[英]React has detected a change in the order of Hooks?
I keep getting this error saying "ERROR Warning: React has detected a change in the order of Hooks called by MainMenuScreen. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks" .我不断收到此错误消息:“错误警告:React 检测到 MainMenuScreen 调用的 Hooks 顺序发生了变化。如果不修复,这将导致错误和错误。有关更多信息,请阅读 Hooks 规则: https://reactjs .org/link/rules-of-hooks” 。 Really confused don't know why am getting this error if someone can please explain or show an example it would greatly be appreciated.
真的很困惑,不知道为什么会出现这个错误,如果有人可以请解释或展示一个例子,将不胜感激。 Many thanks for considering my request.
非常感谢您考虑我的要求。
Here is an image of the code.这是代码的图像。
function MainMenuScreen({ navigation, route, props }) {
const globalContext = useContext(Context)
const { setIsLoggedIn, appSettings, domain, userObj, setUserObj, setToken, address, setAddress } = globalContext;
const [selecedTab, setSelectedTab] = React.useState(tabs[0]);
return (
<View style={styles.container}>
<LinearGradient colors={['gold', '#FF7F50', '#FF7F50']} style={StyleSheet.absoluteFill}>
<FlatList
data={tabs}
horizontal
showsHorizontalScrollIndicator={false}
style={{ flexGrow: 0 }}
keyExtractor={(item, index) => `${item}-${index}`}
renderItem={({ item: tab }) => {
return (
<TouchableOpacity onPress={() => setSelectedTab(tab)}>
<View style={[styles.pill,
{
backgroundColor: selecedTab === tab ? 'gold' : 'transparent',
},
]}>
<Text style={[styles.pillText, { color: selecedTab === tab ? 'white' : 'black' }]}>{tab}</Text>
</View>
</TouchableOpacity>
)
}}
/>
<FlatList
data={popularFood}
keyExtractor={item => item.key}
renderItem={({ item }) => {
return (
<View style={{ flexDirection: 'row' }}>
<Image source={{ uri: item.image }} style={{ width: 100, height: 100, margin: 10 }} />
<View>
<Text style={{ fontSize: 20, fontWeight: 'bold' }}>{item.type}</Text>
<View>
<AntDesign name="star" size={20} color="gold" style={{ marginRight: 10 }} />
<Text style={{ fontSize: 20, fontWeight: 'bold' }}>{item.rating}</Text>
</View>
</View>
</View>
)
}}
/>
<Text style={styles.title}>Address</Text>
<Text style={styles.title}>{address}</Text>
</LinearGradient>
</View>
);
};
Error:
ERROR Warning: React has detected a change in the order of Hooks called by MainMenuScreen. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: https://reactjs.org/link/rules-of-hooks
It actually happens because of any bad practice for hook implementations它实际上是由于钩子实现的任何不良做法而发生的
1 - Only Call Hooks at the Top Level Don't call Hooks inside loops, conditions, or nested functions. 1 - 仅在顶层调用 Hook 不要在循环、条件或嵌套函数中调用 Hook。 Instead, always use Hooks at the top level of your React function
相反,请始终在 React 函数的顶层使用 Hooks
NOTE: Implement your useState hooks first at top of the function
注意:首先在函数顶部实现你的 useState 钩子
2 - Only Call Hooks from React Functions Don't call Hooks from regular JavaScript functions 2 - 仅从 React 函数调用 Hooks 不要从常规 JavaScript 函数调用 Hooks
3 - Getting Error during Test If you get This Error when testing the component, be careful to where you set your custom hooks (replace to top of the function) 3 - 测试期间出现错误如果在测试组件时遇到此错误,请注意设置自定义挂钩的位置(替换到函数顶部)
Best Practice use eslint for lint your code avoid geting React Hooks Rules errors最佳实践使用 eslint 对代码进行 lint,避免出现 React Hooks Rules 错误
install package with npm or yarn使用 npm 或 yarn 安装包
npm install eslint-plugin-react-hooks --save-dev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.