[英]onPress and onChangeText doing nothing in React Native
My onPress
& onChangeText
functions doing nothing. 我的
onPress
和onChangeText
函数什么也不做。 If I entered the value, I got this error message 如果输入值,则会收到此错误消息
_this.setState is not a function. (In '_this.setState({
username: username
})', '_this.setState' is undefined)
app/index.js 应用程序/ index.js
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
username: "",
password: ""
};
this._handlePress = this._handlePress.bind(this)
}
_handlePress = () => {
const { username } = this.state;
const { password } = this.state;
Alert.alert(username);
Alert.alert(password);
onSignIn().then(() => navigation.navigate("SignedIn")); //also not working
}
/**/
render() {
/**/
}
}
app/screens/SignIn.js 应用程序/屏幕/ SignIn.js
import React from "react";
export default ({navigation, _handlePress}) => (
<View style={{ paddingVertical: 20 }}>
<Card title="SIGN IN">
<FormLabel>Email</FormLabel>
<FormInput
placeholder="Email address..."
onChangeText={username => this.setState({username})}
/>
<FormLabel>Password</FormLabel>
<FormInput
secureTextEntry
placeholder="Password..."
onChangeText={password => this.setState({password})}
/>
<Button
buttonStyle={{ marginTop: 20 }}
backgroundColor="#03A9F4"
title="SIGN IN"
onPress={this._handlePress}
/>
</Card>
</View>
);
Reference: https://github.com/datomnurdin/auth-reactnative 参考: https : //github.com/datomnurdin/auth-reactnative
you are getting this error as SignIn.js js is just a function and not a Component class. 您会收到此错误,因为SignIn.js js只是一个函数,而不是Component类。 so the function's 'this' does not have any method 'setstate'.
因此函数的“ this”没有任何“ setstate”方法。 You need to write the component within a react class like this
您需要像这样在react类中编写组件
class Signin extends React.Component{
<View style={{ paddingVertical: 20 }}>
<Card title="SIGN IN">
<FormLabel>Email</FormLabel>
<FormInput
placeholder="Email address..."
onChangeText={username => this.setState({username})}
/>
<FormLabel>Password</FormLabel>
<FormInput
secureTextEntry
placeholder="Password..."
onChangeText={password => this.setState({password})}
/>
<Button
buttonStyle={{ marginTop: 20 }}
backgroundColor="#03A9F4"
title="SIGN IN"
onPress={this._handlePress}
/>
</Card>
</View>
}
// i have cloned your repo and run it successfully, by moving username
// and password from index.js to SignIn.js, rewrite SignIn in class way,
// navigation works well(jump to signed successfuly after pressing).
// however not sure if this is what you need.
import React, {Component} from "react";
import { View, Alert } from "react-native";
import { Card, Button, FormLabel, FormInput } from "react-native-elements";
export default class SignIn extends Component {
constructor (props) {
super(props);
this.state = {
username: '',
password: ''
}
}
_handlePress () {
let {navigation} = this.props;
navigation.navigate("SignedIn")
}
_handleUChange (username) {
this.setState({username})
}
_handlePChange (password) {
this.setState({password})
}
render () {
return (
<View style={{ paddingVertical: 20 }}>
<Card title="SIGN IN">
<FormLabel>Email</FormLabel>
<FormInput
placeholder="Email address..."
onChangeText={this._handleUChange.bind(this)}
/>
<FormLabel>Password</FormLabel>
<FormInput
secureTextEntry
placeholder="Password..."
onChangeText={this._handlePChange.bind(this)}
/>
<Button
buttonStyle={{ marginTop: 20 }}
backgroundColor="#03A9F4"
title="SIGN IN"
onPress={this._handlePress.bind(this)}
/>
</Card>
</View>
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.