简体   繁体   English

反应本机:.! 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用

[英]React Native !!! Error: Invalid hook call. Hooks can only be called inside of the body of a function component

The error: Error: Invalid hook call.错误:错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app你可能在同一个应用程序中拥有多个 React 副本
    de of the body of a function component. function 组件主体的 de。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:
  4. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  5. You might be breaking the Rules of Hooks3.你可能违反了 Hooks3 的规则。 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.您可能在同一个应用程序中拥有多个 React 副本,请参阅https://reactjs.org/link/invalid-hook-call以获取有关如何调试和修复此问题的提示。 ERROR TypeError: null is not an object (evaluating 'dispatcher.useState') ERROR Invariant Violation: "main" has not been registered.错误类型错误:null 不是 object(评估“dispatcher.useState”)错误不变违规:“main”尚未注册。 This can happen if:* Metro (the local dev server) is run from the wrong folder.在以下情况下可能会发生这种情况:* Metro(本地开发服务器)从错误的文件夹运行。 Check if Metro is running, stop it and restart it in the current project.检查 Metro 是否正在运行,将其停止并在当前项目中重新启动。
  • A module failed to load due to an error and AppRegistry.registerComponent wasn't called.由于错误而无法加载模块并且未调用AppRegistry.registerComponent

My code我的代码

**import * as React from 'react';
import {useState} from 'react';
import { Text, StyleSheet, 
KeyboardAvoidingView, ScrollView, Image,
TextInput, TouchableOpacity, View } from 'react-native';
import { CheckBox } from 'react-native-elements';
import {Ionicons} from '@expo/vector-icons'

const [input, setInput] = useState('');
const [hidePass, setHidePass] = useState(true);
export default class App extends React.Component {
 

    constructor(props) {
      super(props);
      this.state = {
          ischecked1: true
      };
  }
    onChangeCheck1() {
      this.setState({ ischecked1: !this.state.ischecked1 });
    }
    render() {
      
        return (
          
            <KeyboardAvoidingView
            style={styles.container}
             >
            <ScrollView>
              
                <Image
                    source={require('./assets/logo.png')}
                    style={styles.logo}
                    />
                <Text style={styles.helloText}> 
                Olá de novo !
                </Text>
               <Text style={styles.welcomeText}>
                  Bem-vindo(a) de volta,
                  sentimos sua falta! 
                </Text>
               
                <TextInput
                style={styles.inputArea}
                placeholder="Digite o e-mail"
                />
                <TextInput
                style={styles.inputArea}
                placeholder="Senha"
                value={input}
                onChangeText={ (texto) => setInput(texto)}
                secureTextEntry={hidePass}
                />
                <TouchableOpacity style={styles.eye} onPress={ () =>
                   setHidePass(!hidePass) }>
                  <Ionicons name={hidePass ? 'eye' : 'eye-off'}
                   color="#A0D800" size={25}/>
                </TouchableOpacity>
                <View style={styles.checkBoxStyle}>
                <CheckBox
                  left
                  size={18}
                  checkedColor='#A0D800'
                  value={this.state.ischecked1}
                  checked={this.state.ischecked1}
                  onPress={this.onChangeCheck1.bind(this)}
                  containerStyle={{ backgroundColor: "transparent",
                  borderColor: "transparent", marginRight: 0}}
                />
              <Text style={styles.Connected}> 
                Manter-se conectado
              </Text>
                <Text style={styles.forgotPassword}> 
                  Esqueci minha senha
                </Text>
                
                </View>
                
          
                <TouchableOpacity 
                    style={styles.botao}
                    /*onPress={ () => {this.clicou()} }*/
                >
                    <Text style={styles.botaoText}>Entrar</Text>
                </TouchableOpacity>
                
                
            </ScrollView>
            </KeyboardAvoidingView>
            
        );
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 2,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#fff'
    },
    logo: {      
        marginTop:50,
        marginBottom: 80,
        width: 150,
        height: 40,
        
    },
    inputArea:{
       marginTop: 30,
       padding: 15,
       height: 60,
       width: 370,
       borderColor: '#808080',
       borderWidth: 1,
       backgroundColor: '#fff',
       fontSize: 16,
       fontWeight: 'bold',
       borderRadius: 15
    },
    botao: {
        width: 350,
        height: 60,
        backgroundColor: '#000000',
        marginTop: 50,
        marginLeft: 8,
        borderRadius: 15,
        alignItems: 'center',
        justifyContent: 'center'
    },
    botaoText: {
      fontSize: 15,
      fontWeight: 'bold',
      color: '#fff'
    },
    helloText: {
      fontSize: 40,
      fontWeight: 'bold',
      marginTop: 15,
      color: '#000000',
      marginEnd: 120,
      marginTop: 8
    },
    welcomeText: {
      fontSize: 16,
      marginTop: 10,
      marginEnd: 35,
      marginVertical: 10,
      color: '#808080',
    },
    forgotPassword: {
      textDecorationLine: 'underline',
      fontWeight: 'bold',
      marginTop: 15,
      marginBottom: 15,
      marginLeft: 30,
      fontSize: 12
    },
    Connected:{
      textDecorationLine: 'underline',
      fontWeight: 'bold',
      marginTop: 15,
      fontSize: 12,
      marginRight: 55,
      marginLeft: -5
    },
    checkBoxStyle:{
      marginTop: 15,
      flexDirection: 'row',
      marginStart: -10
   },
   eye:{
   alignSelf: 'flex-end',
   bottom: 42,
   right: 40
   }
})**

The error is pretty straight forward.错误非常简单。 You have a class extending React.Component, and you define a state in the constructor, why would you need the useState hook?您有一个扩展 React.Component 的 class,并且您在构造函数中定义了一个 state,为什么需要 useState 挂钩? You have to choose one or the other, function components using hooks, or class components.您必须选择一个或另一个,使用挂钩的 function 组件或 class 组件。 Functions starting by useXXX are hooks, it's a naming convention.以 useXXX 开头的函数是钩子,它是一种命名约定。

See here for more information about hooks: https://reactjs.org/docs/hooks-intro.html有关挂钩的更多信息,请参见此处: https://reactjs.org/docs/hooks-intro.html

Taken from this page:取自此页面:

Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.

Removing the 2 lines above your class declaration should fix the problem.删除 class 声明上方的 2 行应该可以解决问题。

Please people correct me if I'm wrong, but it seems to me that you can't use both because of how React keeps track of states, needing a single reference to the value to watch.如果我错了,请人们纠正我,但在我看来,你不能同时使用这两者,因为 React 如何跟踪状态,需要对要观察的值的单一引用。

Hope I helped.希望我有所帮助。

You have to choose between class component or functional component.您必须在 class 组件或功能组件之间进行选择。

Here, you have a class component, but useState can be used only in a functional component.在这里,您有一个 class 组件,但 useState 只能在功能组件中使用。

I have rewritten your component as functional, you can try to replace all you component by this:我已将您的组件重写为功能性组件,您可以尝试通过以下方式替换所有组件:

import * as React from 'react';
import {useState} from 'react';
import { Text, StyleSheet, 
KeyboardAvoidingView, ScrollView, Image,
TextInput, TouchableOpacity, View } from 'react-native';
import { CheckBox } from 'react-native-elements';
import {Ionicons} from '@expo/vector-icons'

const App = () => {
const [input, setInput] = useState('');
const [hidePass, setHidePass] = useState(true);
const [ischecked1, setIschecked1] = useState(true)

const onChangeCheck1 = () => {
    setIschecked1(!ischecked1)
}
  
return (
    
    <KeyboardAvoidingView
        style={styles.container}
    >
        <ScrollView>
            <Image
                source={require('./assets/logo.png')}
                style={styles.logo}
            />
            <Text style={styles.helloText}> 
                Olá de novo !
            </Text>
            <Text style={styles.welcomeText}>
                Bem-vindo(a) de volta,
                sentimos sua falta! 
            </Text>
            
            <TextInput
                style={styles.inputArea}
                placeholder="Digite o e-mail"
            />
            <TextInput
                style={styles.inputArea}
                placeholder="Senha"
                value={input}
                onChangeText={ (texto) => setInput(texto)}
                secureTextEntry={hidePass}
            />
            <TouchableOpacity style={styles.eye} onPress={ () => setHidePass(!hidePass) }>
                <Ionicons name={hidePass ? 'eye' : 'eye-off'}
                    color="#A0D800" size={25}
                />
            </TouchableOpacity>
            <View style={styles.checkBoxStyle}>
                <CheckBox
                    left
                    size={18}
                    checkedColor='#A0D800'
                    value={ischecked1}
                    checked={ischecked1}
                    containerStyle={{ backgroundColor: "transparent",
                    borderColor: "transparent", marginRight: 0}}
                />
                <Text style={styles.Connected}> 
                    Manter-se conectado
                </Text>
                <Text style={styles.forgotPassword}> 
                    Esqueci minha senha
                </Text>
            </View>
            
            <TouchableOpacity 
                style={styles.botao}
                /*onPress={ () => {this.clicou()} }*/
            >
                <Text style={styles.botaoText}>Entrar</Text>
            </TouchableOpacity>
        </ScrollView>
    </KeyboardAvoidingView>
);

}
const styles = StyleSheet.create({
container: {
    flex: 2,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff'
},
logo: {      
    marginTop:50,
    marginBottom: 80,
    width: 150,
    height: 40,
    
},
inputArea:{
   marginTop: 30,
   padding: 15,
   height: 60,
   width: 370,
   borderColor: '#808080',
   borderWidth: 1,
   backgroundColor: '#fff',
   fontSize: 16,
   fontWeight: 'bold',
   borderRadius: 15
},
botao: {
    width: 350,
    height: 60,
    backgroundColor: '#000000',
    marginTop: 50,
    marginLeft: 8,
    borderRadius: 15,
    alignItems: 'center',
    justifyContent: 'center'
},
botaoText: {
  fontSize: 15,
  fontWeight: 'bold',
  color: '#fff'
},
helloText: {
  fontSize: 40,
  fontWeight: 'bold',
  marginTop: 15,
  color: '#000000',
  marginEnd: 120,
  marginTop: 8
},
welcomeText: {
  fontSize: 16,
  marginTop: 10,
  marginEnd: 35,
  marginVertical: 10,
  color: '#808080',
},
forgotPassword: {
  textDecorationLine: 'underline',
  fontWeight: 'bold',
  marginTop: 15,
  marginBottom: 15,
  marginLeft: 30,
  fontSize: 12
},
Connected:{
  textDecorationLine: 'underline',
  fontWeight: 'bold',
  marginTop: 15,
  fontSize: 12,
  marginRight: 55,
  marginLeft: -5
},
checkBoxStyle:{
  marginTop: 15,
  flexDirection: 'row',
  marginStart: -10
},
eye:{
alignSelf: 'flex-end',
bottom: 42,
right: 40
}
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native:错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native: Error: Invalid hook call. Hooks can only be called inside of the body of a function component 反应原生。 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native. Error: Invalid hook call. Hooks can only be called inside of the body of a function component 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component React 17:错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React 17: Error: Invalid hook call. Hooks can only be called inside of the body of a function component 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 通过路由反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React 反应,得到错误:无效的钩子调用。 Hooks 只能在函数组件的主体内部调用 - React, getting Error: Invalid hook call. Hooks can only be called inside of the body of a function component React - 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component “错误:无效的钩子调用。只能在 function 组件的主体内部调用钩子。”在反应原生 expo - "Error: Invalid hook call. Hooks can only be called inside of the body of a function component.." in react native expo 收到此错误“无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。” 在反应功能组件内? - Getting this error “Invalid hook call. Hooks can only be called inside of the body of a function component.” inside a react functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM