[英]onChange doesn't work with mobile? - React Native
Right now I am trying to make a Log In Page and I've been using React Native to do so.现在我正在尝试制作一个登录页面,并且我一直在使用 React Native 来做到这一点。 It works perfectly fine on a web browser but when I try it on my phone, onChange doesn't seem to change the state of the password and username.
它在 web 浏览器上运行良好,但是当我在手机上尝试时,onChange 似乎并没有更改密码和用户名的 state。
import React from 'react';
import { TextInput, View, TouchableOpacity, Text } from 'react-native';
class LogIn extends React.Component {
constructor(props){
super(props);
this.state={
username: 'John Doe',
password: 'abc123'
}
}
loginChangeHandler = (event) =>{
this.setState({[event.target.name]: event.target.value});
}
loginButtonHandler = () =>{
alert('username: '+this.state.username+ ' Password: '+this.state.password)
}
render() {
return (
<View style = {{alignItems: 'center'}}>
<TextInput name = 'username' onChange = {this.loginChangeHandler}
placeholder = 'username'
style={{ width: 200, height: 55, borderColor: 'gray', borderWidth: 1 }}
/>
<Text>{'\n'}</Text>
<TextInput name = 'password' onChange = {this.loginChangeHandler} secureTextEntry={true}
placeholder = 'password'
style={{ width: 200, height: 55, borderColor: 'gray', borderWidth: 1 }}
/>
<Text>{'\n'}</Text>
<TouchableOpacity onPress = {this.loginButtonHandler} style = {{height: 45, width: 200, justifyContent: 'center', alignItems: "center", backgroundColor: 'green'}}>
<Text style = {{fontSize: 16, color: 'white'}}>LOG IN</Text>
</TouchableOpacity>
</View>
);
}
}
export default LogIn;
To use this
inside of a function you must bind the function with this
object.要在 function 内部使用
this
,您必须将 function 与this
object 绑定。 Other thing is there isn't direct name
prop for TextInput
component and event.target
is not returning something you expected.另一件事是
TextInput
组件没有直接的name
道具,并且event.target
没有返回您期望的内容。 onChange is called with { nativeEvent: { eventCount, target, text} }
, then i suggest you to use onChangeText
callback. onChange 是用
{ nativeEvent: { eventCount, target, text} }
调用的,那么我建议你使用onChangeText
回调。 This callback is called with only the value which you are typing.仅使用您输入的值调用此回调。
Change your code this way.以这种方式更改您的代码。
render() {
return (
<View style = {{alignItems: 'center'}}>
<TextInput name = 'username' onChangeText= {(value) => this.setState({ username : value })}
placeholder = 'username'
style={{ width: 200, height: 55, borderColor: 'gray', borderWidth: 1 }}
/>
<Text>{'\n'}</Text>
<TextInput name = 'password' onChangeText={(value) => this.setState({ password : value })} secureTextEntry={true}
placeholder = 'password'
style={{ width: 200, height: 55, borderColor: 'gray', borderWidth: 1 }}
/>
<Text>{'\n'}</Text>
<TouchableOpacity onPress = {this.loginButtonHandler.bind(this)} style = {{height: 45, width: 200, justifyContent: 'center', alignItems: "center", backgroundColor: 'green'}}>
<Text style = {{fontSize: 16, color: 'white'}}>LOG IN</Text>
</TouchableOpacity>
</View>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.