[英]Cannot get material UI Textfield data
I'm quite a newbie with React.我是 React 的新手。 I made a login page using firebase auth and have any input field to get the person's contact to validate but I can't get this data.
我使用 firebase auth 创建了一个登录页面,并有任何输入字段来验证此人的联系人,但我无法获取此数据。 Already tried everything I found here but it is not working.
已经尝试了我在这里找到的所有东西,但它不起作用。
I tried the ref={x => this.contacto = x}
as shown below but not working.我尝试了
ref={x => this.contacto = x}
,如下所示,但不起作用。
export class Login extends Component {
handleClick=()=>{
const recaptcha = new firebase.auth.RecaptchaVerifier('recaptcha');
const number = this.contacto.value;
console.log(number)
firebase.auth().signInWithPhoneNumber(number, recaptcha).then( function(e) {
var code = prompt('Enter the otp', '');
if(code === null) return;
e.confirm(code).then(function (result) {
console.log(result.user);
document.querySelector('label').textContent += result.user.phoneNumber + "Number verified";
}).catch(function (error) {
console.error( error);
});
})
.catch(function (error) {
console.error( error);
});
}
render(){
return (
<div>
<div id="recaptcha"></div>
<Grid container justify="center">
<Grid item component={Card} xs={6} md={3} className={cx(styles.card)}>
<CardContent>
<img className={styles.image} src={image} alt="cmcq" />
<Typography variant="h6" className={cx(styles.titulo)} gutterBottom>
Login
</Typography>
<TextField
id="outlined-primary"
label="Insert phone number"
variant="outlined"
color="primary"
size="small"
ref={x => this.contacto = x}
className={cx(styles.field)}
/>
<Button variant="contained" color="primary" className={cx(styles.button)} onClick={this.handleClick} >
Entrar
</Button>
</CardContent>
</Grid>
</Grid>
</div>
);
}
};
export default Login;
Try using the onChange handler for TextField to set a state variable.尝试使用 TextField 的 onChange 处理程序来设置 state 变量。 You can reference it with
this.state.varName
您可以使用
this.state.varName
引用它
onTextChange = (event) => {
this.setState({varName: event.target.value});
}
<TextField
id="outlined-primary"
label="Insert phone number"
variant="outlined"
color="primary"
size="small"
onChange={this.onTextChange}
className={cx(styles.field)}
/>
You shouldn't use ref
to access data of TextField
, you should use onChange
callback method to access data, try:你不应该使用
ref
来访问TextField
的数据,你应该使用onChange
回调方法来访问数据,尝试:
<TextField
onChange={e => console.log(e)}
/>
The logged e
contains your expected data.记录的
e
包含您的预期数据。 For using ReactJS mindset you should use state
and add it to the state and then access to it.要使用 ReactJS 思维方式,您应该使用
state
并将其添加到 state 然后访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.