简体   繁体   English

如何在本机反应中仅添加 3 个文本输入

[英]How to add only 3 Text inputs in react native

I have to add 3 text input but when i press the "add more skills" button it added more than 3 text inputs I just want inputs to be no more than three我必须添加 3 个文本输入,但是当我按下“添加更多技能”按钮时,它添加了超过 3 个文本输入我只想输入不超过三个

这是要添加 3 个输入的移动屏幕

here is my code for input handler这是我的输入处理程序代码

const [inputs, setInputs] = useState([{ key: '', value: '' }]);

    const addHandler = () => {
        const addinputs = [...inputs];
        addinputs.push({ key: '', value: '' });
        setInputs(addinputs);
        if ( addinputs == '3') {
            showError("Can't add more than 3")
         }
        
    }
    const deleteHandler = (key) => {
        const _inputs = inputs.filter((input, index) => index != key);
        setInputs(_inputs);
    }
    const inputHandler = (text, key) => {
        const _inputs = [...inputs];
        _inputs[key].value = text;
        _inputs[key].key = key;
        setInputs(_inputs);
       
    }

here is the code of return for styling这是样式的返回代码

 <View>

                                <View >
                                    {/* <ScrollView > */}
                                    {inputs.map((input, key) => (
                                        <View >
                                            <TextInput
                                                style={GlobalSS.textInput}
                                                outlineColor='grey'
                                                mode='outlined'
                                                maxLength={15}
                                                activeOutlineColor='black'
                                                placeholder={"Other Skills"}
                                                value={input.value}
                                                onChangeText={(text) => inputHandler(text, key)} />

                                            <TouchableOpacity onPress={() => deleteHandler(key)}>
                                                <Text style={styles.delete}>Delete Skills</Text>
                                            </TouchableOpacity>
                                        </View>
                                    ))}
                                    {/* </ScrollView> */}

                                    <TouchableOpacity
                                        onPress={addHandler}>
                                        <Text style={styles.moreskills}>Add more Skills</Text>
                                    </TouchableOpacity>

                                </View>
                            </View>

for live editing in code you can go to this link https://snack.expo.dev/@muhammadabdullahrishi/add-input对于代码中的实时编辑,您可以转到此链接https://snack.expo.dev/@muhammadabdullahrishi/add-input

Just add a check on addHandler that will not trigger if input list has three elements in it.只需在 addHandler 上添加一个检查,如果输入列表中包含三个元素,则该检查不会触发。 Update your code as follows:更新您的代码如下:

const addHandler = () => {
    if(inputs.length <3){

    const addinputs = [...inputs];
    addinputs.push({ key: '', value: '' });
    setInputs(addinputs);
    }
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native-如何添加10个文本输入并获取总计 - React native - How to add 10 text inputs and get the total 如何在本机反应中添加动态输入? - How to add dynamic inputs in react native? 如何在React Native中为图像添加文本? - How to add text to an image in React Native? React Native:在循环然后显示的文本输入数组中,如何获取第n个元素并分别修改这些元素? - React Native: In an array of Text Inputs which are looped then displayed, how to get nth element and modify those elements separately? 如何将 Speech-to-Text 添加到使用 React Native 编写的聊天应用程序中 - How to add Speech-to-Text into a chat application that is written on React Native 编辑结束后如何在TextInput中添加文本React Native - How to add text in TextInput after editing ends React Native 如何在 React Native 中动态添加或删除文本输入 - How to dynamically add or remove a Text Input in React Native 在REACT-NATIVE上将扫描条形码的类型或数据写入文本输入 - writing type or data of scanned barcode into text inputs on REACT-NATIVE 在文本中添加指向文本的链接 - React Native - Add a link to a text within the text - React Native 如何仅使用JavaScript将输入添加到表单 - How to add inputs to a form only with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM