Reactjs 将值从true设置为false
我有一个值为true,但当提交时输入为空时,我需要将其设置为false,因为某些原因,我无法将值从true设置为false 这是我的代码:Reactjs 将值从true设置为false,reactjs,react-native,Reactjs,React Native,我有一个值为true,但当提交时输入为空时,我需要将其设置为false,因为某些原因,我无法将值从true设置为false 这是我的代码: const [cellError, setcellError] = useState(true); const [passError, setpassError] = useState(true); CheckTextInput = () => { if (vCellphone != '') { onSubmit({ vCel
const [cellError, setcellError] = useState(true);
const [passError, setpassError] = useState(true);
CheckTextInput = () => {
if (vCellphone != '') {
onSubmit({ vCellphone, vPassword, loading })
} else {
setcellError(false);
}
}
};
return (
<View style={styles.container}>
<View style={styles.inputContainer}>
<TextInput style={styles.inputs}
placeholder="Teléfono"
underlineColorAndroid='transparent'
onChangeText={newvCellphone => setvCellphone(newvCellphone)}
keyboardType={'numeric'}
value={vCellphone}
autoCorrect={false}
autoCompleteType = "off"
maxLength = {10}
/>
</View>
{cellError != true ?
<Text style={{ color: "red" }}> Se requiere numero de telefono</Text> : null}
<TouchableOpacity style={[styles.buttonContainer, styles.loginButton]}
onPress={() => CheckTextInput()}
>
<Text style={styles.loginText}>INGRESAR</Text>
</TouchableOpacity>
</View>
);
};
const[cellError,setcellError]=useState(true);
const[passeror,setpasseror]=useState(true);
CheckTextInput=()=>{
如果(vCellphone!=''){
onSubmit({vCellphone,vPassword,loading})
}否则{
setcellError(假);
}
}
};
返回(
设置手机(新手机)}
键盘类型={'numeric'}
值={vCellphone}
自动更正={false}
autoCompleteType=“关闭”
maxLength={10}
/>
{cellError!=真?
所需电话号码:空}
CheckTextInput()}
>
安格尔
);
};
由于某种原因,setcellError(false)
和setpassError(false)
不起作用,有什么建议吗?将onPress={()=>CheckTextInput()}
更改为onPress={this.CheckTextInput}
更改
cellError!=true
到cellError==true
您需要使用useffect
来更新组件
您可以尝试:
useEffect(() => {
if (vCellphone === '') setcellError(false)
}, [vCellphone])
你为什么说它不工作?这个逻辑向后看。你不想在
cellError==true
时显示错误消息吗?它工作,但不工作它很奇怪,当我加载我的页面时,bot输入触发验证(我不想这样,我想用按钮触发验证的状态),另一件事是,当我按下按钮时,第二次输入将删除警告。