间歇性 React 未捕获错误:对象作为 React 子项无效
我有一个 Header 组件,它会根据用户是否登录来呈现一些链接。
此组件加载正常,可以正常显示登录和注册链接。单击这些链接会将我带到这些组件,这些组件可以正常运行。登录后,标题会更改为显示退出链接。
但是,当我退出时,事情就出错了。登录和注册链接显示正常,但不再起作用。单击注册链接会出现以下错误:
Uncaught Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of
Signin
.
再次单击该链接会产生以下错误:
bundle.js:4297 Uncaught TypeError: Cannot read property 'getHostNode' of null
我在这里读过类似的问题,通常它们涉及有人试图呈现对象而不是其属性之一。但是,这里的情况并非如此 - 还是我遗漏了什么?
我的代码:
标头:
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Link} from 'react-router';
class Header extends Component{
renderLinks(){
if(this.props.authenticated){
return <li className="nav-item ">
<Link className="nav-link" to="/signout">Sign Out</Link>
</li>
}else{
return [<li className="nav-item page-scroll" key={1}>
<Link className="nav-link page-scroll" to="/signin">Sign In</Link>
</li>,
<li className="nav-item" key={2}>
<Link className="nav-link page-scroll" to="/signup">Sign Up</Link>
</li>
]
}
}
render() {
return (
<div className="container-fluid header">
<nav className="navbar navbar-toggleable-md navbar-inverse bg-inverse">
<div className="navbar-nav">
<Link to="/" className="navbar-brand">
<img alt="Trellis" src="../../images/trellis.png" />
</Link>
<ul className="nav navbar-nav">
{this.renderLinks()}
</ul>
</div>
</nav>
</div>
);
}
}
function mapStateToProps(state){
return{
authenticated: state.auth.authenticated
};
}
export default connect(mapStateToProps)(Header);
登录组件:
import React, { Component } from 'react';
import { reduxForm, Field } from 'redux-form';
import * as actions from '../../actions';
import { connect } from 'react-redux';
import { Link } from 'react-router';
class Signin extends Component {
handleFormSubmit({ email, password }) {
this.props.signInUser({email, password});
}
renderInput(field) {
if(!field.className) { field.className = "form-control" }
if(!field.type) { field.type = "text" }
return (
<div>
<Field name={field.name} id={field.name} type={field.type} className={field.className} component="input" />
</div>
)
}
renderAlert(){
if(this.props.errorMessage){
return(
<div className="alert alert-danger">
<strong>Oops!</strong> {this.props.errorMessage}
</div>
);
}
}
render() {
const { handleSubmit } = this.props
return (
<form className="sign-in" onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
<fieldset className="form-group">
<label>Email:</label>
{this.renderInput({ name: "email", type: "email" })}
</fieldset>
<fieldset className="form-group">
<label>Password:</label>
{this.renderInput({ name: "password", type: "password" })}
</fieldset>
<div className="password-forgot">
<Link to="/reset-password">I forgot my password</Link>
</div>
{this.renderAlert()}
<button action="submit" className="btn btn-primary">Sign in</button>
</form>
)
}
}
function mapStateToProps(state){
return{errorMessage: state.auth.error};
}
Signin = connect(mapStateToProps, actions)(Signin)
export default reduxForm({
form: 'signin'
})(Signin)
退出组件:
import React, {Component} from 'react';
import {connect} from 'react-redux';
import * as actions from '../../actions';
class Signout extends Component {
componentWillMount() {
this.props.signOutUser();
}
render() {
return <div>Sorry to see you go!</div>;
}
}
export default connect(null, actions)(Signout);
登录操作:
export function signInUser(props) {
const { email, password } = props;
return function(dispatch){
axios.post(`${ROOT_URL}/signin`, {email, password})
.then(response => {
localStorage.setItem('user', JSON.stringify(response.data));
dispatch({ type: AUTH_USER });
browserHistory.push('/dashboard');
})
.catch((err) => {
console.log(JSON.stringify('login error:', err));
dispatch(authError(SIGNIN_FAILURE, 'Bad login credentials'));
});
};
}
退出操作:
export function signOutUser() {
localStorage.clear();
return { type: UNAUTH_USER };
}
(根据原始帖子的评论)
问题出在 SignIn 渲染组件中,您调用
renderAlert()
函数,该函数返回
this.props.errorMessage
作为 JSX 元素的一部分。这是 SignIn 渲染的 JSX 中唯一可能出现问题的部分(根据您的代码)。
结果发现
this.props.errorMessage
实际上是一个对象,而不是您所想的字符串,因此出现有关对象作为 React 子项的错误。