getElementById react

编程入门 行业动态 更新时间:2024-10-09 20:25:33
本文介绍了getElementById react-native的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何获取本机元素?

我的用例是一个登录屏幕.按下提交按钮,现在我想获取用户名和密码TextInputs的值.

My use-case is a login screen. the submit button is pressed and now i want to get the value of username and password TextInputs.

export default class Login extends Component { login() { //document.getElementById('username'); //run-time error } render() { return ( <View style={{flex: 1,flexDirection: 'column',justifyContent: 'space-between',margin:10}}> <View style={{backgroundColor: 'powderblue'}} /> <MyTextField id='user' placeholder="User Name" /> <MyTextField id='pass' placeholder="Password" /> <MyButton onPress={this.login} title='Login'/> </View> ); } }

推荐答案

您没有在本地响应中获取getElementById,必须使用状态.您可以这样做:

You don't have get getElementById in react native, you have to use state. you can do like this:

export default class Login extends Component { constructor (props) { super(props); this.state={ email:'', password:'' } this.login = this.login.bind(this); // you need this to be able to access state from login } login() { console.log('your email is', this.state.email); console.log('your password is', this.state.password); } render() { return ( <View style={{flex: 1,flexDirection: 'column',justifyContent: 'space-between',margin:10}}> <View style={{backgroundColor: 'powderblue'}} /> <TextInput onChangeText={(email) => { this.setState({email})/> <TextInput secureTextEntry={true} onChangeText={(password) => { this.setState({password})/> <MyButton onPress={this.login} title='Login'/> </View> ); } }

更多推荐

getElementById react

本文发布于:2023-11-28 20:15:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:getElementById   react

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!