javascript - 如何根据用户从 Picker 中选择不显示 TextInputs 并将 textinput 的值存储在数组中
问题描述
我允许用户从选择器中选择一个数字,它可以随机选择1、4、6 。当他选择像 3 这样的数字时,将显示 3 个 TextInputs,他将在 3 个输入字段中输入一些内容,我想将这些值保存到一个数组中。怎么能做到这一点,react native
我认为我使用了一种不好的方法,我想要一个可以使我的代码高效并告诉我如何将值存储到数组中的专家。问候
{this.state.noOfStores === 1 && (
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
)}
{this.state.noOfStores === 2 && (
<View>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
</View>
)}
{this.state.noOfStores === 3 && (
<View>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
</View>
)}
{this.state.noOfStores === 4 && (
<View>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
<TextInput
style={[
styles.input,
{
backgroundColor: theme.colors.lightGray,
},
]}
placeholder=" Name "
keyboardType={'default'}
placeholderTextColor="gray"
/>
</View>
)}
解决方案
使用这个我希望它能解决你的问题。 如果您在以下代码方面需要任何帮助,请告诉我。
import React, {Component} from 'react';
import {View, TouchableOpacity, Text, TextInput} from 'react-native';
export class AddInputs extends Component {
state = {
textInput: [],
inputData: [],
};
//function to add TextInput dynamically
addTextInput = index => {
let textInput = this.state.textInput;
textInput.push(
<TextInput
style={{
height: 40,
width: '100%',
borderColor: '#2B90D8',
borderBottomWidth: 3,
}}
placeholder={'Add Text'}
onChangeText={text => this.addValues(text, index)}
/>,
);
this.setState({textInput});
};
//function to add text from TextInputs into single array
addValues = (text, index) => {
let dataArray = this.state.inputData;
let checkBool = false;
if (dataArray.length !== 0) {
dataArray.forEach(value => {
if (value.index === index) {
value.text = text;
checkBool = true;
}
});
}
if (checkBool) {
this.setState({
inputData: dataArray,
});
} else {
dataArray.push({text: text, index: index});
this.setState({
inputData: dataArray,
});
}
};
render() {
return (
<View
style={{
flex: 1,
}}>
<View
style={{
width: '100%',
alignItems: 'center',
}}>
{this.state.textInput.map(value => {
return value;
})}
</View>
<TouchableOpacity
onPress={() => {
this.addTextInput(
'your desired number of inputs here like 5, 20 etc',
);
}}>
<Text>Add Inputs</Text>
</TouchableOpacity>
</View>
);
}
}
推荐阅读
- javascript - 如何在安卓应用中使用 OCR(光学字符识别)
- python - 当我的烧瓶网页处于“GET”方法时,它无法连接到通常在“POST”方法中执行的任何静态文件
- python - 根据特定条件在数据框行中添加值
- angular - ngx-image-cropper - 如何仅更改裁剪器的长度或宽度
- python - 如果在 groupby 之后,熊猫的 .sum 似乎忽略了 skipna=False
- vue.js - SPA 静态 Nuxt 应用程序在通过共享主机部署时无法获取静态文件,但在本地服务器上运行良好
- oracle - 如何在 SQL Server 中从 Oracle 实现类似于 for 循环的代码?
- html - 当我使用'a href'时如何固定宽度和高度百分比?
- ios - PreferredContentSizeCategory 的设置器
- r - 使列标题跨越子组