护照登录名_login

时间:2019-01-22 21:22:52

标签: javascript reactjs passport.js

在我的反应护照应用程序的提交表单页面上,提交给我一个错误this.props._login()不是函数。

我尝试将_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: '/'
        })
    }

1 个答案:

答案 0 :(得分:0)

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

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

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

您必须像下面这样将构造函数的第一个参数转发到super()

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