简体   繁体   English

无法在本机反应中获取 TextInput 值

[英]Can't get TextInput value in react native

Hi friends I hope you are doing very well ^^ I am struggling to get a TextInput value when a user enters it in a simple basic form I've created, here is the field:嗨朋友们,我希望你做得很好^^当用户以我创建的简单基本表单输入 TextInput 值时,我正在努力获取它,这里是该字段:

<TextInput placeholder='latitude' name='lat'/>
<Button style={styles.signIn}
    title="Show my entered text"
      onPress={() => {
      alert(this.state.lat)}}
  />

I tried so many methods to get the value of this field but unfortunately none works.. I am new to react native so I really don't know how can this be done properly (is there any variable I need to define) because in javascript I used to get it only by the GetElementById quite simple right XD I tried creating a variable exactly bellow the imports like this:我尝试了很多方法来获取该字段的值,但不幸的是没有一个有效..我是新手,所以我真的不知道如何正确完成(是否需要定义任何变量),因为在 javascript我过去只能通过 GetElementById 非常简单的权利 XD 来获得它,我尝试在导入的下面创建一个变量,如下所示:

value={this.state.lat} 

So I can get the above textInput actual value entered by the user but it isn't working at all and my alert function is not returning anything (I actually don't know if alert() is a react native integrated function or I need to define it from scratch:/), please help I am really lost I tried many solutions on the web but nothing resolves my problem.所以我可以获得用户输入的上述 textInput 实际值,但它根本不起作用,我的警报 function 没有返回任何内容(我实际上不知道 alert() 是否是反应原生集成 function 或者我需要从头开始定义它:/),请帮助我真的迷路了我在 web 上尝试了许多解决方案,但没有任何解决方案可以解决我的问题。 Thanks in advance提前致谢

Try this sample code试试这个示例代码

class Example extends React.Component {
  state = { lat: "" };

  render() {
    return (
      <View>
        <TextInput
          placeholder="latitude"
          name="lat"
          value={this.state.lat}
          onChangeText={(text) => this.setState({ lat: text })}
        />
        <Button
          style={styles.signIn}
          title="Show my entered text"
          onPress={() => {
            alert(this.state.lat);
          }}
        />
      </View>
    );
  }
}

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

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