Javascript 使用道具时更改TextInput中的值时出现问题
我有一个显示两个文本输入的类,最初值是空的,但是另一个组件通过道具发送值。问题是,我也想在这个组件中编辑它们,但它不能正常工作。在收到值并更改它们之后,如果我想用按钮复制密码并粘贴它,则不复制值。奇怪的是,它正确地复制了用户,尽管我记得昨天发生了相反的事情(复制密码)。我添加了userInitialized和passInitialized布尔值,如果通过键入而不是传递道具来更改值,则这些布尔值将设置为true。有什么想法吗?谢谢Javascript 使用道具时更改TextInput中的值时出现问题,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有一个显示两个文本输入的类,最初值是空的,但是另一个组件通过道具发送值。问题是,我也想在这个组件中编辑它们,但它不能正常工作。在收到值并更改它们之后,如果我想用按钮复制密码并粘贴它,则不复制值。奇怪的是,它正确地复制了用户,尽管我记得昨天发生了相反的事情(复制密码)。我添加了userInitialized和passInitialized布尔值,如果通过键入而不是传递道具来更改值,则这些布尔值将设置为true。有什么想法吗?谢谢 import React, { Component } from
import React, { Component } from 'react';
import { View, Text, StyleSheet, TextInput, Clipboard, Button } from 'react-native';
export default class BotomQRScanner extends Component{
constructor(props){
super(props);
this.state = {
userField: this.props.user,
userInitialized: false,
passInitialized: false,
passwordField: this.props.pass
}
}
handleUserChange = value => {
this.setState({userField: value, userInitialized: true});
}
handlePasswordChange = value => {
this.setState({passwordField: value, passInitialized: true});
}
render(){
return (
<View style={{height: '100%', width: '100%', alignItems: 'center', justifyContent: 'space-around'}}>
<View style={{flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center'}}>
<TextInput
onChangeText={(value) => this.handleUserChange(value)}
style={{textAlign: 'center', fontSize: 16, height: 50, width: 200}}
autoCapitalize = 'none'
autoCorrect={false}
selectTextOnFocus={true}
placeholder={'username'}
selectionColor={'#428AF8'}
value={this.state.userInitialized? this.state.userField : this.props.user}
/>
<Button
title="copy"
onPress={() => {
Clipboard.setString(this.state.userInitialized? this.state.userField : this.props.user)
}}
/>
</View>
<View style={{flexDirection: 'row', justifyContent: 'space-around', alignItems: 'center'}}>
<TextInput
onChangeText={(value) => this.handlePasswordChange(value)}
style={{textAlign: 'center', fontSize: 16, height: 80, width: 200}}
autoCapitalize = 'none'
autoCorrect={false}
selectTextOnFocus={true}
placeholder={'password'}
value={this.state.passInitialized? this.state.passField : this.props.pass}
/>
<Button
title="copy"
onPress={() => {
Clipboard.setString(this.state.passInitialized? this.state.passField : this.props.pass)
}}
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
logo: {
width: 80,
height: 26,
resizeMode: 'contain',
},
container: {
padding: 10,
flexDirection: 'row',
},
right: {
flex: 1,
flexDirection: 'row',
justifyContent: 'flex-end',
}
})
import React,{Component}来自'React';
从“react native”导入{视图、文本、样式表、文本输入、剪贴板、按钮};
导出默认类BotomQRScanner扩展组件{
建造师(道具){
超级(道具);
此.state={
userField:this.props.user,
userInitialized:false,
密码初始化:false,
密码字段:this.props.pass
}
}
handleUserChange=值=>{
this.setState({userField:value,userInitialized:true});
}
handlePasswordChange=值=>{
this.setState({passwordField:value,passInitialized:true});
}
render(){
返回(
this.handleUserChange(值)}
样式={{textAlign:'center',fontSize:16,高度:50,宽度:200}
自动资本化='无'
自动更正={false}
选择textonfocus={true}
占位符={'username'}
selectionColor={'#428AF8'}
value={this.state.userInitialized?this.state.userField:this.props.user}
/>
{
剪贴板.setString(this.state.userInitialized?this.state.userField:this.props.user)
}}
/>
this.handlePasswordChange(值)}
样式={{textAlign:'center',fontSize:16,高度:80,宽度:200}
自动资本化='无'
自动更正={false}
选择textonfocus={true}
占位符={'password'}
value={this.state.passInitialized?this.state.passField:this.props.pass}
/>
{
剪贴板.setString(this.state.passInitialized?this.state.passField:this.props.pass)
}}
/>
);
}
}
const styles=StyleSheet.create({
标志:{
宽度:80,
身高:26,
resizeMode:'包含',
},
容器:{
填充:10,
flexDirection:'行',
},
对:{
弹性:1,
flexDirection:'行',
justifyContent:“柔性端”,
}
})
您在代码中混合了变量名:
Clipboard.setString(this.state.passInitialized? this.state.passField : this.props.pass)
这里您使用的是passField
,但在您的状态中您声明了passwordField
这是一个包含代码的工作沙箱:哦,谢谢你!效果确实更好!我现在只遇到一个新问题,执行以下操作:
1。我在输入2中手动记录。我从道具发送新值3。文本输入不显示新值