首页 > 解决方案 > 护照登录问题_login

问题描述

在我提交的反应护照应用程序的注册表单页面上,它给了我一个错误 this.props._login() is not a function。

我尝试将 _login 更改为登录并摆脱道具

class LoginForm extends Component {
    constructor() {
        super()
        this.state = {
            username: '',
            password: '',
            redirectTo: null
        }
        // this.googleSignin = this.googleSignin.bind(this)
        this.handleSubmit = this.handleSubmit.bind(this)
        this.handleChange = this.handleChange.bind(this)
    }

    handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        })
    }

    handleSubmit(event) {
        event.preventDefault()
        console.log('handleSubmit')
        this.props._login(this.state.username, this.state.password)
        this.setState({
            redirectTo: '/'
        })
    }

标签: javascriptreactjspassport.js

解决方案


在您的构造函数中,您只设置默认状态:

constructor() {
    super()
    this.state = {
        username: '',
        password: '',
        redirectTo: null
    }
    // ...
}

但是您永远不会将传递的道具作为第一个参数读取,因此您的this.props值永远不会被定义。

您必须将构造函数的第一个参数转发为super()这样:

constructor(props) {
    super(props)
    this.state = {
        username: '',
        password: '',
        redirectTo: null
    }
    // ...
}

推荐阅读