[英]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 个文本输入我只想输入不超过三个
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.