0

私が作成した反応コンポーネントには、次のコンストラクターと関数があります。

constructor() {
    super()
    this.state = {
        error: false
    }
}

handleSubmit(e) {
    e.preventDefault()
    const email = this.refs.email.value
    const password = this.refs.password.value
    const self = this

    firebase.auth().signInWithEmailAndPassword(email, password).then(
        function(result) {
            const location = self.props.location
            if (location.state && location.state.nextPathname) {
                self.context.router.replace(location.state.nextPathname)
            } else {
                self.context.router.replace("/home")
            }
            // User signed in!
            console.log("User signed in!")
    }).catch(function(error) {
        this.setState({error: error}) // Error points to this line
    })
}

コンソールに次のエラーが表示され続けます。

キャッチされていない TypeError: 未定義のプロパティ 'setState' を読み取ることができません

誰でも私が問題を特定するのを助けることができますか?

4

1 に答える 1

3

次のコードでは、'self' を使用する必要がある場合に 'this' を使用しています。

catch(function(error) {
    this.setState({error: error}) // Error points to this line
})

する必要があります

catch(function(error) {
    self.setState({error: error}) // Error points to this line
})
于 2016-10-27T14:41:33.190 に答える