每当我在 React/Redux 应用程序上提交登录尝试时,都会收到此错误。浏览器窗口弹出的完整错误如下:
Error: Element type is invalid: expected a string (for built-in components)or a class/function
(for composite components) but got: object. You likely forgot to export your component from
the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `Router.Consumer`.
它当然也显示了它认为发生错误的代码块,但正如我们所知,这并不总是准确的,这是其中之一。它显示的块来自 loginAction.js 文件,看起来像这样...
import DOMAIN from "../constants/domain"
export default function loginAction(state){
let user = null
let login_destination = DOMAIN() + 'login'
console.log("Before the fetch, the request is... POST " + login_destination)
let login_object = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
username: state.username_input,
password: state.password_input
})
}
return (dispatch) => {
fetch(login_destination, login_object)
.then(resp => resp.json())
.then(json => user = json)
.then(user => {
console.log("Inside the LoginAction.js file. In final .then() block once a user model has been fetched")
console.log(user)
let userObj = {user: user.user, stocks: user.user_stocks}
console.log(userObj) // THIS IS WHERE THE COMPILER SAYS THE ERROR OCCURS
if (typeof(user) != "string"){
dispatch({type: 'USER_LOGIN', payload: userObj})
}
else{
dispatch({type: 'USER_LOGIN', payload: user})
}
})
}
}
我在网上看到很多人说这样的错误通常是对导入/导出问题造成的,但正如您在这里看到的,loginAction.js 文件设置为导出只有一件事,接收动作的文件看起来像这样......
import React, {Component} from 'react';
import { connect } from 'react-redux';
import loginAction from '../../dispatch_actions/loginAction.js';
import Signup from '../dispatchers/Signup';
import './css/LoginOrSignUp.css'
const mapStateToProps = (state) => {
return({
failed_attempt: state.user.failed_attempt
})
}
const mapDispatchToProps = (dispatch) => {
return({
submitLogin: (state) => {
dispatch({type: 'LOAD_LOGIN'})
dispatch(loginAction(state))
}
})
}
上面显示的文件中当然有更多代码,但除了登录按钮之外,没有其他任何东西在这里太疯狂或令人困惑。还值得一提的是,调度程序确实到达了减速器,因为我在那里有一个 console.log 语句,它不断被命中,这里也是该文件,以防万一它可能有帮助,因为最后的 console.og 语句是最后一件事在错误之前被击中
export default function manageUser(
state=
{
failed_attempt: {failed: false, reason: null}, // if a login attemot fails, this will be used to prompt a message
isLoggedIn: false, // determine whether these is a current user
loading: false, // true if fetching user from a login or signup
name: null, // user's name
email: null, // Kind of obvious
gen_code: null, // Exists only as a code emailed to a user upon sign up. Expires in 90 seconds
targets: [null], // stocks that user has targetted. Will be represented as an object array
}, action){
switch(action.type){
// {type: LOAD_LOGIN}
case('LOAD_LOGIN'):
return ({
...state, loaidng: true
})
// {type: "FAILURE", payload: <string> }
case('FAILURE'):
return ({
...state, failed_attempt: {failed: true, reason: action.payload}
})
// {type: "USER_LOGIN", payload: <userObj from Rails Server>}
// userObj will look like this...
//
// _____________________________________________________________________________
// | user: |
// | {username: <input>, password_digest: <hidden>} |
// | stocks: |
// | [ <array of ints> ] |
// |___________________________________________________________________________|
//
//
case('USER_SIGNUP'):
case('USER_LOGIN'):
console.log("Inside User Reducer, LOGIN Case. Payload below")
console.log(action.payload)
return({
...state, failed_attempt: {failed: false, reason: null}, isLoggedIn: true, loading: false, name: action.payload.user.username, stocks: action.payload.stocks
})
default:
return ({
...state
})
}
}```