Javascript 按下“反应本机”按钮不工作
按钮onPress不起作用,根本没有调用方法onButtonPress()。我不确定我做错了什么 我有一个未打印的console.log语句。 下面的代码来自Stephen Grider的react native udemy cource 下面是loginForm.js的代码 任何帮助都将不胜感激Javascript 按下“反应本机”按钮不工作,javascript,reactjs,react-native,Javascript,Reactjs,React Native,按钮onPress不起作用,根本没有调用方法onButtonPress()。我不确定我做错了什么 我有一个未打印的console.log语句。 下面的代码来自Stephen Grider的react native udemy cource 下面是loginForm.js的代码 任何帮助都将不胜感激 import React, { Component } from 'react'; import { Text } from 'react-native'; import firebase from
import React, { Component } from 'react';
import { Text } from 'react-native';
import firebase from 'firebase';
import { Button, Card, CardSection, Input, Spinner, Field } from './common';
class LoginForm extends Component {
state = { email: '', password: '', error: '', loading: false };
onButtonPress() {
const { email, password } = this.state;
console.log('da');
this.setState({ error: '', loading: true });
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(this.onLoginSuccess.bind(this))
.catch(() => {
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then(this.onLoginSuccess.bind(this))
.catch(this.onLoginFail.bind(this));
});
}
onLoginFail() {
this.setState({
error: 'Authentication Failed',
loading: false
});
}
onLoginSuccess() {
this.setState({
email: '',
password: '',
loading: false,
error: ''
});
}
renderButton() {
if (this.state.loading) {
return <Spinner size="small" />;
}
return <Button onPress={this.onButtonPress.bind(this)}>Log in</Button>;
}
render() {
return (
<Card>
<CardSection>
<Field
placeholder="user@gmail.com"
label="Email"
value={this.state.email}
onChangeText={email => this.setState({ email })}
/>
</CardSection>
<CardSection>
<Field
secureTextEntry
placeholder="password"
label="Password"
value={this.state.password}
onChangeText={password => this.setState({ password })}
/>
</CardSection>
<Text style={styles.errorTextStyle}>{this.state.error}</Text>
<CardSection>{this.renderButton()}</CardSection>
</Card>
);
}
}
const styles = {
errorTextStyle: {
fontSize: 20,
alignSelf: 'center',
color: 'red'
}
};
export default LoginForm;
import React,{Component}来自'React';
从“react native”导入{Text};
从“firebase”导入firebase;
从“/common”导入{按钮、卡片、卡片节、输入、微调器、字段};
类LoginForm扩展组件{
状态={电子邮件:“”,密码:“”,错误:“”,加载:false};
onButtonPress(){
const{email,password}=this.state;
console.log('da');
this.setState({错误:“”,正在加载:true});
火基
.auth()
.使用电子邮件和密码登录(电子邮件、密码)
.then(this.onloginsucess.bind(this))
.catch(()=>{
火基
.auth()
.createUserWithEmailAndPassword(电子邮件,密码)
.then(this.onloginsucess.bind(this))
.catch(this.onLoginFail.bind(this));
});
}
onLoginFail(){
这是我的国家({
错误:“身份验证失败”,
加载:错误
});
}
onloginsucess(){
这是我的国家({
电子邮件:“”,
密码:“”,
加载:false,
错误:“”
});
}
renderButton(){
if(this.state.loading){
返回;
}
返回登录;
}
render(){
返回(
this.setState({email})}
/>
this.setState({password})}
/>
{this.state.error}
{this.renderButton()}
);
}
}
常量样式={
errorTextStyle:{
尺寸:20,
对齐自我:“中心”,
颜色:“红色”
}
};
导出默认登录信息;
您需要将此
绑定到渲染按钮
,您可以使用自动绑定此
的箭头功能
renderButton = () => { //Arrow function binds this automatically
if (this.state.loading) {
return <Spinner size="small" />;
}
return <Button onPress={this.onButtonPress.bind(this)}>Log in</Button>;
}
renderButton=()=>{//Arrow函数自动绑定此属性
if(this.state.loading){
返回;
}
返回登录;
}
请将按钮代码贴在这里。``````从“React”导入React;从“react native”导入{Text,TouchableOpacity};const Button=props=>{return({props.children});}```哎呀,这是按钮代码中的一个错误。谢谢你的提示。LoginFrom的现有代码没有任何更改。实际上我在按钮代码上犯了一个错误。我收到的道具不正确。