javascript - TypeError: Object(...) is not a function in React when dispatch a login function
问题描述
我正在学习一个教程,但我遇到了这个我似乎无法弄清楚的错误。我正在尝试将参数分派给 AuthActions.js 中的登录函数,但我不断收到错误TypeError: Object(...) is not a function当我提交时。我的代码如下
这是 AuthActions.js 中的代码
export const login = (email, pass) => {
return {
type: 'LOGIN',
payload: {email, pass}
}
}
然后这是登录组件中的代码
import React, {Component} from 'react'
import {connect} from "react-redux";
import Field from "../Field";
import {login} from "../../store/actions/AuthActions";
const fields = [
{label: 'Username/Email', placeholder: 'Enter Your Username/Email', elementName: 'input', type: 'email', name: 'email', id: 'user'},
{label: 'Password', placeholder: 'Enter Your Password', elementName: 'input', type: 'password', name: 'password', id: 'password'}
]
class Login extends Component{
render(){
return(
<div className='col-lg-8 mx-auto'>
<form onSubmit={e => {
e.preventDefault();
this.props.login(this.props.values.email, this.props.values.password);
}}>
{fields.map((field, fieldIndex) => {
return(
<Field
{...field}
key={fieldIndex}
value={this.props.values[field.name]}
name={field.name}
onChange={this.props.handleChange}
onBlur = {this.props.handleBlur}
touched = {(this.props.touched[field.name])}
errors = {(this.props.errors[field.name])}
/>
)
})
}
<br/>
<div id='success'/>
<div className='form-group'>
<button className='btn btn-primary btn-l'>Login</button>
</div>
</form>
</div>
)
}
}
const mapStateToProps = state => {
return {
auth: state.auth
}
}
const mapDispatchToProps = dispatch => {
return {
//this is where the error point to
login: (email, pass) => dispatch(login(email, pass))
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Login);
它抛出这个错误
Uncaught TypeError: Object(...) is not a function
login Login.js:71
onSubmit Login.js:30
React 17
unstable_runWithPriority scheduler.development.js:653
React 4
关于这个功能
login: (email, pass) => dispatch(login(email, pass))
这也是减速机
const defaultState = {
user: {},
token: null
}
const auth = (state = defaultState, action) => {
switch(action.type){
case 'LOGIN':
return{
...state,
user: action.payload,
token: 1
}
default:
return state
}
}
export default auth
解决方案
下面的代码正在工作。(我完全使用了你的减速器和动作)
class Login extends Component{
render(){
return(
<div className='col-lg-8 mx-auto'>
<label>{ this.props.auth?.user.email }</label>
<button onClick={e => {
e.preventDefault();
this.props.login('email', 'password');
}}>CLICK</button>
</div>
)
}
}
const mapStateToProps = state => {
return {
auth: state
}
}
const mapDispatchToProps = dispatch => {
return {
//this is where the error point to
login: (email, pass) => dispatch(login(email, pass))
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Login);
如果您通过组合减速器来完成它:例如
减速器/index.js
import { combineReducers } from 'redux';
import authed from './authed';
export default combineReducers({
authed,
})
你应该改变你mapStateToProps
的如下:
const mapStateToProps = state => {
return {
auth: state.authed
}
}
推荐阅读
- excel - Excel Vba 表值,有谁知道如何在这些单元格中写入正确的值?
- kubernetes - 如何将一个不同的 Secret 挂载到 StatefulSet 管理的每个 Pod 中?
- java - logger 在课堂上的位置应该是什么?
- java - 将位图图像存储到对象 Anroid
- php - 从现有文件夹恢复静态应用程序
- excel - 如何找到不同年份和日期的宿舍
- java - javac 11 可以编译将在 java 8 JVM 上运行的二进制文件/jar 吗?
- c++ - live555 会在 NAL 单元发出之前解析它们吗
- java - glDrawElements 上的 GL_INVALID_ENUM
- javascript - 在运行时查找当前处于滚动状态的 div。-苹果浏览器