[英]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:
这可能由于以下原因之一而发生:
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.