简体   繁体   English

React Native textInput 清除

[英]React Native textInput clear

I have a textinput like this:我有一个这样的文本输入:

 <TextInput 
          secureTextEntry={true}
          autoCompleteType="password"
          onChangeText={val => setClave(val)}
/>

and the component is:组件是:

const [clave, setClave] = useState('');

which is just for the password, because it is a login screen.这只是密码,因为它是一个登录屏幕。 So i press a button and execute some functions that validates the textInput data, and when is all ready, navigate to another screen,所以我按下一个按钮并执行一些验证 textInput 数据的功能,当一切就绪时,导航到另一个屏幕,

my final function that navigates to another screen is this:我最终导航到另一个屏幕的 function 是这样的:

const goInicio = () => {
    setClave('')        
    navigation.navigate('Home')
    
  };

and what i tried to do there is to set the state of clave to an empty string but if i navigate back to the login screen the password will still be written, even though its value changed, and it no longer works to log in, it is still showing as if it has something written, how can i fix that?我试图做的是将clave的state设置为一个空字符串,但是如果我导航回登录屏幕,密码仍然会被写入,即使它的值发生了变化,并且它不再适用于登录,它仍然显示好像写了一些东西,我该如何解决?

The internal value state of TextInput and clave are unrelated since you have not set clave to be the value prop of TextInput . TextInputclave的内部值 state 是不相关的,因为您没有将clave设置为TextInputvalue prop You are changing clave in the onChange method of the TextInput but not the other way around.您正在TextInputonChange方法中更改clave ,但不是相反。 This direction is one directional.这个方向是一个方向。

Instead, set the value prop of the TextInput to be equal to clave相反,将TextInputvalue属性设置为clave

<TextInput 
  value={clave}
  secureTextEntry={true}
  autoCompleteType="password"
  onChangeText={val => setClave(val)}
/>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM