【发布时间】:2019-08-07 07:35:11
【问题描述】:
我在 React native 中创建了动态表单,如果用户单击该按钮,它将在按钮上方添加新表单。用户单击按钮的次数将添加那么多表单。我已经做到了,但在文本更改过程中遇到了问题。
问题是当我开始输入表单时,它首先显示秒数的值,然后变为空
下面是我的代码我试过了
import React, { PureComponent } from 'react'
import {
Text,
View,
TextInput
} from 'react-native'
import { TouchableOpacity } from 'react-native-gesture-handler';
export class IndentForm extends PureComponent {
constructor(props) {
super(props);
this.state = {
indents:[]
};
}
addInput(){
this.setState({ indents:[...this.state.indents, ""]})
}
onTextChange = (event, index) => {
const { indent } = event.nativeEvent;
this.state.indents[index] = indent
this.setState({indent})
}
render() {
return (
<View style={{flex:1}}>
{
this.state.indents.map((indent,index) => {
return(
<View key={index}>
<TextInput
name="indent"
style={{height: 40,backgroundColor: 'azure', fontSize: 20}}
placeholder="Type here to translate!"
value={indent}
onChange={(event)=> this.onTextChange(event,index)}
/>
</View>
)
})
}
<TouchableOpacity
style={{ height:30, width:70, backgroundColor:'blue'}}
onPress={this.addInput.bind(this)}>
<Text>Add Form</Text>
</TouchableOpacity>
</View>
)
}
}
export default IndentForm
【问题讨论】:
-
它在几分之一秒内显示什么值?
-
@V.Sambor 我输入的值
-
在新输入中还是在之前创建的输入中?
-
@V.Sambor 最初没有表单,一旦我单击按钮,它将添加新表单,第一个表单正常工作,然后当我再次单击按钮添加第二个表单时,第二个表单有问题,但是当我添加第三种形式第二种形式工作正常,第三种形式给出问题等等
-
您使用
onChange而不是onChangeText的任何具体原因?
标签: javascript reactjs react-native