Reactjs 为什么我';“我得到了”;TypeError:this.props.login不是函数";即使我的登录是一个函数

Reactjs 为什么我';“我得到了”;TypeError:this.props.login不是函数";即使我的登录是一个函数,reactjs,Reactjs,使用正确的用户名和密码登录时,会弹出此错误 TypeError:this.props.login不是函数 这是action/auth.js在此处输入代码 export const login = (username, password) => dispatch => { const config = { headers: { "Content-Type": "application/json" } }; const body = JSON.s

使用正确的用户名和密码登录时,会弹出此错误

TypeError:this.props.login不是函数

这是action/auth.js<代码>在此处输入代码

export const login = (username, password) => dispatch => {
  const config = {
    headers: {
      "Content-Type": "application/json"
    }
  };

  const body = JSON.stringify({ username, password });

  axios
    .post("http://localhost:8000/api/auth/login", body, config)
    .then(res => {
      dispatch({ type: LOGIN_SUCCESS, payload: res.data });
    })
    .catch(err => {
      console.log(err.message);
    });
};
在这里呼叫登录

import React, { Component } from "react";
import { Link, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import { login } from "../../actions/auth";

class Login extends Component {
  state = {
    username: "",
    password: ""
  };

  static propTypes = {
    login: PropTypes.func.isRequired,
    isAuthenticated: PropTypes.bool
  };

  onSubmit = e => {
    e.preventDefault();
    this.props.login(this.state.username, this.state.password);
    console.log("submited");
  };

  onChange = e => this.setState({ [e.target.name]: e.target.value });

  render() {
    if (this.props.isAuthenticated) {
      return <Redirect to="/" />;
    }

    const { username, password } = this.state;
    return (
      <div className="col-md-6 m-auto">

          <form onSubmit={this.onSubmit}>

          </form>
        </div>

    );
  }
}

const mapStateToProps = state => ({
  isAuthenticated: state.auth.isAuthenticated
});

export default connect(mapStateToProps)(Login);
import React,{Component}来自“React”;
从“react router dom”导入{Link,Redirect};
从“react redux”导入{connect};
从“道具类型”导入道具类型;
从“../../actions/auth”导入{login};
类登录扩展组件{
状态={
用户名:“”,
密码:“
};
静态类型={
登录名:PropTypes.func.isRequired,
IsAuthentication:PropTypes.bool
};
onSubmit=e=>{
e、 预防默认值();
this.props.login(this.state.username、this.state.password);
控制台日志(“提交”);
};
onChange=e=>this.setState({[e.target.name]:e.target.value});
render(){
如果(此.props.isAuthenticated){
返回;
}
const{username,password}=this.state;
返回(
);
}
}
常量mapStateToProps=状态=>({
isAuthenticated:state.auth.isAuthenticated
});
导出默认连接(MapStateTops)(登录);
它没有正确导入,但我的路径是正确的


我被要求使用正确的用户名和密码登录,但无法登录。

您没有将
login()
作为道具传递。您可以通过
mapDispatchToProps()
传递它,类似于您的
mapStateToProps()

const mapDispatchToProps = dispatch => ({
  login: (username, password) => {
    dispatch(login(username, password));
  },
});

...

export default connect(mapStateToProps, mapDispatchToProps)(Login);

您没有将
login()
作为道具传递。您可以通过
mapDispatchToProps()
传递它,类似于您的
mapStateToProps()

const mapDispatchToProps = dispatch => ({
  login: (username, password) => {
    dispatch(login(username, password));
  },
});

...

export default connect(mapStateToProps, mapDispatchToProps)(Login);

但是
login
不是道具,因为您从未通过父级或更高级别的组件将其作为道具传入。它只是
login(this.state.username,this.state.password)
您将登录函数作为道具传递给组件的地方?你能不能也添加代码?你能展示一下你将动作连接到组件的代码片段吗?现在我发布了完整的代码。但是
login
不是一个道具,因为你从来没有通过父级或更高级别的组件将其作为一个道具传入。它只是
login(this.state.username,this.state.password)
您将登录函数作为道具传递给组件的地方?你能不能也添加这些代码?你能展示一下你把动作连接到组件上的代码片段吗?现在我发布了完整的代码。我看到了,愚蠢到忘了这个来传递连接。我看到了,愚蠢到忘了这个来传递连接。