简体   繁体   English

无法在React项目中读取未定义的属性“值”?

[英]Cannot read property 'value' of undefined in react project?

Im try to create an Login using react js and firebase. 我尝试使用react js和firebase创建一个Login。 When I try to compile this it gives an error like below 当我尝试编译它时,它给出如下错误

TypeError: Cannot read property 'value' of undefined

It's that I'm try to display user inputs in console. 我试图在控制台中显示用户输入。

My Login.jsx file like below 我的Login.jsx文件如下所示

import React, { Component } from 'react';

class Login extends Component {

    constructor(props){
        super(props);
        this.authWithEmailPassword = this.authWithEmailPassword.bind(this);
    }

    authWithEmailPassword(event) {
        event.preventDefault();
        console.log("authed with email");
        console.table([{
            email: this.emailInput.value,
            password: this.passwordInput.value
        }])
    }

    render(){
        return(
            <div style={loginStyle}>
                <hr style={{marginTop: "10px", marginBottom: "10px"}} />
                <form onSubmit={(event)=> {this.authWithEmailPassword(event) }}
                ref={(form) => { this.loginForm = form }}>
                    <div style={{ marginBottom: "10px" }} className="pt-callout pt-icon-info-sign">
                        <h5>Note</h5>
                        If you dont have an account, this form will create your account 
                    </div>
                    <label className="pt-label">
                        Email
                        <input style={{width: "100%"}} className="pt-input" name="email" type="email"
                        ref="{(input) => {this.emailInput=input}}" placeholder="Email"/>
                    </label>

                    <label className="pt-label">
                        Password
                        <input style={{width: "100%"}} className="pt-input" name="password" type="password"
                        ref="{(input) => {this.passwordInput=input}}" placeholder="Password"/>
                    </label>
                    <input type="submit" style={{width: "100%"}} className="pt-button pt-intent-primary" value="Login"/>
                </form>
            </div>
        );
    }
}

export default Login;

What did I do wrong Please help me in this matter Im still a newbie. 我做错了什么事,请帮助我。我还是新手。 How can I fix this 我怎样才能解决这个问题

Remove double quotes in ref variable. 删除ref变量中的双引号。 It should be 它应该是

ref={(input) => {this.passwordInput = input}}

Change your refs ref={(input) => this.emailInput = input} 更改您的裁判ref={(input) => this.emailInput = input}

ref={(input) => this.passwordInput = input}

below is the demo code with your code 下面是带有您的代码的演示代码

demo with your code 用你的代码演示

For better use cases, use state or props over ref to fetch form data 为了获得更好的用例,请在ref上使用state或props来获取表单数据

reference : use-state-or-refs-in-react-js-form-components 参考: 使用状态或refs-react-js-form-components

change your code with this 改变你的代码

<input style={{width: "100%"}} className="pt-input" name="email" type="email"ref="{(input) => this.emailInput=input}" placeholder="Email"/>

<input style={{width: "100%"}} className="pt-input" name="password" type="password" ref="{(input) => this.passwordInput=input}" placeholder="Password"/>

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

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