猿问

如何根据用户从 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"

                />


蝴蝶不菲
浏览 106回答 3
3回答

梵蒂冈之花

使用这个我希望它能解决你的问题。&nbsp;如果您在遵循代码方面需要任何帮助,请告诉我。import React, {Component} from 'react';import {View, TouchableOpacity, Text, TextInput} from 'react-native';export class AddInputs extends Component {&nbsp; state = {&nbsp; &nbsp; textInput: [],&nbsp; &nbsp; inputData: [],&nbsp; };&nbsp; //function to add TextInput dynamically&nbsp; addTextInput = index => {&nbsp; &nbsp; let textInput = this.state.textInput;&nbsp; &nbsp; textInput.push(&nbsp; &nbsp; &nbsp; <TextInput&nbsp; &nbsp; &nbsp; &nbsp; style={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 40,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: '100%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; borderColor: '#2B90D8',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; borderBottomWidth: 3,&nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; placeholder={'Add Text'}&nbsp; &nbsp; &nbsp; &nbsp; onChangeText={text => this.addValues(text, index)}&nbsp; &nbsp; &nbsp; />,&nbsp; &nbsp; );&nbsp; &nbsp; this.setState({textInput});&nbsp; };&nbsp; //function to add text from TextInputs into single array&nbsp; addValues = (text, index) => {&nbsp; &nbsp; let dataArray = this.state.inputData;&nbsp; &nbsp; let checkBool = false;&nbsp; &nbsp; if (dataArray.length !== 0) {&nbsp; &nbsp; &nbsp; dataArray.forEach(value => {&nbsp; &nbsp; &nbsp; &nbsp; if (value.index === index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value.text = text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkBool = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; if (checkBool) {&nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; inputData: dataArray,&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; dataArray.push({text: text, index: index});&nbsp; &nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; &nbsp; inputData: dataArray,&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; };&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <View&nbsp; &nbsp; &nbsp; &nbsp; style={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flex: 1,&nbsp; &nbsp; &nbsp; &nbsp; }}>&nbsp; &nbsp; &nbsp; &nbsp; <View&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style={{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: '100%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alignItems: 'center',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {this.state.textInput.map(value => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })}&nbsp; &nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; &nbsp; &nbsp; <TouchableOpacity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onPress={() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.addTextInput(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'your desired number of inputs here like 5, 20 etc',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Text>Add Inputs</Text>&nbsp; &nbsp; &nbsp; &nbsp; </TouchableOpacity>&nbsp; &nbsp; &nbsp; </View>&nbsp; &nbsp; );&nbsp; }}

慕桂英546537

你可以这样做:&nbsp; &nbsp; this.state = { values: [] };&nbsp; &nbsp; const textInputs = [];&nbsp; &nbsp; for(let i=0; i< this.state.noOfStores; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textInputs.push(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextInput&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style ={[styles.input,{ backgroundColor: theme.colors.lightGray }]}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder=" Name "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboardType={'default'}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholderTextColor="gray"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChangeText={text => this.onChangeText(text,i)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setState({ values: [...this.state.values, ''] });&nbsp; &nbsp; }&nbsp; &nbsp; onChangeText = (text,index) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const values = [...this.state.values];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values[index] = text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setState({ values });&nbsp; &nbsp; }然后在渲染方法中:<View>&nbsp; &nbsp; &nbsp;{textInputs}</View>

SMILET

你可以像下面这样,根据你的选择器设置一个给定长度的数组,并显示基于它的 UI。setRows 设置行数,您可以轻松更新数组。从您的选择器中调用 setRows。export default function App() {&nbsp; const [textArr, setTextArr] = useState([]);&nbsp;&nbsp;&nbsp; const onChangeText = (text, index) => {&nbsp; &nbsp; const arr = [...textArr];&nbsp; &nbsp; arr[index] = text;&nbsp; &nbsp; setTextArr(arr);&nbsp; };&nbsp; const&nbsp; setRows=(rowCount)=>{&nbsp; &nbsp;setTextArr(Array(rowCount).fill('Default text'))&nbsp; };&nbsp; return (&nbsp; &nbsp; <View style={{ paddingTop: 100 }}>&nbsp; &nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; title="set"&nbsp; &nbsp; &nbsp; &nbsp; onPress={()=>setRows(6)}&nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; {textArr.map((item, index) => (&nbsp; &nbsp; &nbsp; &nbsp; <TextInput&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style={[styles.input, { backgroundColor: 'grey' }]}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder=" Name "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keyboardType={'default'}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholderTextColor="gray"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={item}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChangeText={(text)=>onChangeText(text,index)}&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; ))}&nbsp; &nbsp; </View>&nbsp; );}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答