简体   繁体   English

为什么我会收到“对象作为 React 子项无效”错误?

[英]Why am I getting "Objects are not valid as a React child" error?

I have a lot of files, but I think the problem is coming from my authentication component in React.我有很多文件,但我认为问题出在我在 React 中的身份验证组件。 I basically want to only display a page if the user is logged in otherwise I want to the user to be redirected.我基本上只想在用户登录时显示一个页面,否则我希望用户被重定向。

react-dom.development.js:14887 Uncaught Error: Objects are not valid as a React child (found: object with keys {$$typeof, type, compare, WrappedComponent}). react-dom.development.js:14887 Uncaught Error: Objects are not valid as a React child (found: object with keys {$$typeof, type, compare, WrappedComponent}). If you meant to render a collection of children, use an array instead.如果您打算渲染子集合,请改用数组。

requireAuth.js requireAuth.js

// function that can wrap any component to determine if it is authenticated

import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import { push } from "@lagunovsky/redux-react-router"; // not sure if this correct
export default function requireAuth(Component) {
  class AuthenticationComponent extends React.Component {
    constructor(props) {
      super(props);
      this.checkAuth();
    }
    componentDidUpdate(prevProps, prevState) {
      this.checkAuth();
    }

    checkAuth() {
      // if not authenticated then it is redirected
      if (!this.props.isAuthenticated) {
        const redirectAfterLogin = this.props.location.pathname;
        this.props.dispatch(push(`/login?next=${redirectAfterLogin}`));
      }
    }
    // if authenticated then renders the component
    render() {
      return (
        <div>
          {this.props.isAuthenticated === true ? (
            <Component {...this.props} />
          ) : null}
        </div>
      );
    }
  }

  AuthenticationComponent.propTypes = {
    isAuthenticated: PropTypes.bool.isRequired,
    location: PropTypes.shape({
      pathname: PropTypes.string.isRequired,
    }).isRequired,
    dispatch: PropTypes.func.isRequired,
  };

  // checks isAuthenticated from the auth store
  const mapStateToProps = (state) => {
    return {
      isAuthenticated: state.auth.isAuthenticated,
      token: state.auth.token,
    };
  };

  return connect(mapStateToProps)(AuthenticationComponent);
}

App.js应用程序.js

class App extends Component {
  render() {
    return (
      <div>
        <Root>
          <ToastContainer hideProgressBar={true} newestOnTop={true} />
          <Routes>
            <Route exact path="/" element={<Home />} />
            <Route path="/closet" element={requireAuth(Closet)} />
            <Route path="*" element={<NotFound />} />
          </Routes>
        </Root>
      </div>
    );
  }
}

I have done some digging but I can't find a problem like this.我做了一些挖掘,但找不到这样的问题。

The error is because on this line:错误是因为在这一行:

<Route path="/closet" element={React.createComponent(requireAuth(Closet))} />

You're passing the actual class definition to the element prop and not an instance of the class (which would be the React component).您将实际的class定义传递给element属性,而不是 class 的实例(这将是 React 组件)。 To fix this, you can use React.createElement :要解决此问题,您可以使用React.createElement

class App extends Component {
  render() {
    return (
      <div>
        <Root>
          <ToastContainer hideProgressBar={true} newestOnTop={true} />
          <Routes>
            <Route exact path="/" element={<Home />} />
            <Route path="/closet" element={React.createElement(requireAuth(Closet))} />
            <Route path="*" element={<NotFound />} />
          </Routes>
        </Root>
      </div>
    );
  }
}

Because Route's element props need a ReactNode ,But requireAuth(Closet) 's type is () => React.ReactNode , you can change your App.js like this:因为 Route 的元素道具需要一个ReactNode ,但是requireAuth(Closet)的类型是() => React.ReactNode ,你可以这样改变你的 App.js :


const AuthComponent = requireAuth(Closet);

class App extends Component {
  render() {
    return (
      <div>
        <Root>
          <ToastContainer hideProgressBar={true} newestOnTop={true} />
          <Routes>
            <Route exact path="/" element={<Home />} />
            <Route path="/closet" element={<AuthComponent />} />
            <Route path="*" element={<NotFound />} />
          </Routes>
        </Root>
      </div>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM