gpt4 book ai didi

javascript - getElementById react native

转载 作者:行者123 更新时间:2023-11-30 09:40:37 24 4
gpt4 key购买 nike

在react-native中如何获取元素?

我的用例是登录屏幕。按下提交按钮,现在我想获取用户名和密码 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>

);
}
}

最佳答案

你在react native中没有get getElementById,你必须使用state。你可以这样做:

  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>
);
}
}

关于javascript - getElementById react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41336676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com