我是Redux的新手,是React的半新手。我用它做了一些简单的应用程序。我理解Redux的基本概念(在一个中心位置存储状态)听起来比通过组件/道具传递东西更优雅。
但是,我收到以下错误:
错误:操作可能没有未定义的“类型”属性。你拼错了吗?
onSubmit
src/containers/Login.js:28
25 |
26 | const mapDispatchToProps = (dispatch) => ({
27 | onSubmit: (username, password) => {
> 28 | dispatch(login(username, password))
29 | }
30 | })
31 |
LoginForm._this.onSubmit
src/components/LoginForm.js:29
26 |
27 | onSubmit = (event) => {
28 | event.preventDefault()
> 29 | this.props.onSubmit(this.state.username, this.state.password)
30 | }
31 |
32 | render() {
如您所见,Login.js中的调度行似乎存在问题。
Login.js看起来像这样:
import React from 'react'
import { connect } from 'react-redux'
import { Redirect } from 'react-router'
import LoginForm from '../components/LoginForm'
import {login} from '../actions/auth'
import {authErrors, isAuthenticated} from '../reducers'
const Login = (props) => {
if(props.isAuthenticated) {
return <Redirect to='/' />
}
return (
<div className="login-page">
<LoginForm {...props}/>
</div>
)
}
const mapStateToProps = (state) => ({
errors: authErrors(state),
isAuthenticated: isAuthenticated(state)
})
const mapDispatchToProps = (dispatch) => ({
onSubmit: (username, password) => {
dispatch(login(username, password))
}
})
export default connect(mapStateToProps, mapDispatchToProps)(Login);
我根据我一直在谷歌上搜索的猜测是,我实际上并没有遗漏任何类型的数据,但实际上我的中间件中可能存在问题。
可能会发生什么?我该如何排除故障或解释它?
编辑 - 这是我的登录功能:
export const login = (username, password) => ({
[RSAA]: {
endpoint: '/api/auth/token/obtain/',
method: 'POST',
body: JSON.stringify({username, password}),
headers: { 'Content-Type': 'application/json' },
types: [
LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE
]
}
})
使用redux时,我们需要遵循一定的“样板”。基于错误消息,它只是意味着dispatch
动作期望具有type
属性的对象
dispatch({ type: 'ACTION_TYPE', .... });
所以取决于qazxsw poi的回归,qazxsw poi qazxsw poi将期待一个login(username, password)
类型的对象