【发布时间】:2018-02-17 18:51:38
【问题描述】:
适用于 iOS 和 Android 模拟器
当我开始输入时,文本会消失/闪烁。我尝试让文本具有一些值的初始状态,而不是保持为空。有了这个,TextInput 就会保持这个初始状态,并且不会用输入的新文本进行自我更新。
我认为状态没有使用“onChangeText”属性进行更新,但我不完全确定。
人们似乎已经解决了这个问题,因为他们在代码中几乎没有错别字或缺失的部分。但是我已经彻底检查了我的。
如果我遗漏了以下代码中的任何内容,请提供帮助。
LoginForm.js
import React, { Component } from 'react';
import { Card, Button, CardSection, Input } from './common';
class LoginForm extends Component {
state = { email: '', password: '' }
render() {
return (
<Card>
<CardSection>
<Input
label="Email"
placeHolder="user@gmail.com"
onChangeText={text => this.setState({ email: text })}
value={this.state.email}
/>
</CardSection>
<CardSection>
<Input
secureTextEntry
label="Password"
placeHolder="password"
onChangeText={text => this.setState({ password: text })}
value={this.state.password}
/>
</CardSection>
<CardSection>
<Button>
Log In
</Button>
</CardSection>
</Card>
);
}
}
export default LoginForm;
Input.js
import React from 'react';
import { TextInput, View, Text } from 'react-native';
const Input = ({ label, value, onChangeText, placeholder, secureTextEntry }) => {
const { inputStyle, labelStyle, containerStyle } = styles;
return (
<View style={containerStyle}>
<Text style={labelStyle}>{label}</Text>
<TextInput
secureTextEntry={secureTextEntry}
placeholder={placeholder}
autoCorrect={false}
style={inputStyle}
value={value}
onChangeText={onChangeText}
/>
</View>
);
};
const styles = {
inputStyle: {
color: '#000',
paddingRight: 5,
paddingLeft: 5,
fontSize: 18,
lineHeight: 23,
flex: 2
},
labelStyle: {
fontSize: 18,
paddingLeft: 20,
flex: 1
},
containerStyle: {
height: 40,
flex: 1,
flexDirection: 'row',
alignItems: 'center'
}
};
export { Input };
【问题讨论】:
-
不应该是
onChangeText={e => this.setState({ password: e.target.value })}吗? -
抱歉刚刚注意到您使用的是来自
react-native的TextInput -
@Sagivb.g 没问题。如果问题有任何误导性内容或标签,请告诉我。
-
我会将
onChangeText提取到类方法并调试/记录更改,以确保您获得正确的值并确保问题在于状态未更新跨度> -
发现你的问题,你的
Input组件写错了。我会用答案解释
标签: javascript reactjs react-native