简体   繁体   English

React 检测到 Hooks 的顺序发生了变化?

[英]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.

相关问题 “React 检测到 Hooks 的顺序发生了变化”但是 Hooks 似乎是按顺序调用的 - "React has detected a change in the order of Hooks" but Hooks seem to be invoked in order 收到错误“React 检测到由 StudentsFilter 调用的 Hooks 的顺序发生了变化” - Getting the error " React has detected a change in the order of Hooks called by StudentsFilter" 警告:React 检测到 Table 调用的 Hooks 的顺序发生了变化 - Warning: React has detected a change in the order of Hooks called by Table React 检测到 Hooks 的调用顺序发生了变化,但我看不到任何有条件调用的 hooks - React has detected a change in the order of Hooks called, but I can't see any hooks being called conditonally 为什么我收到此错误“React 检测到 SidebarDesktop 调用的 Hooks 的顺序发生了变化”? - Why I am getting this error “React has detected a change in the order of Hooks called by SidebarDesktop”? React 检测到 TimelineDrawer 调用的 Hooks 的顺序发生了变化。 如果不修复,这将导致错误和错误 - React has detected a change in the order of Hooks called by TimelineDrawer. This will lead to bugs and errors if not fixed 如何将此 class 更改为 function 在 react.js 中有钩子? - How to change this class to function which has hooks in react.js? 更改反应钩子中的处理程序 - Change handlers in react hooks 以正确的顺序放置反应钩子 - Placing react hooks in proper order React 18 中 hooks 的顺序是什么? - What is the order of hooks in React 18?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM