0

Why am I getting this error?

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

My PrivateRoute.js looks like this:

const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={(props) =>
      (Auth.isUserAuthenticated()) ? (
        <Component {...props} {...rest} />
      ) : (
        <Redirect
          to={{
            pathname: "/login",
            state: { from: props.location }
          }}
        />
      )
    }
  />
);

export default PrivateRoute;

My import calls are like this:

import HomePage from "../homepage/homepage";
import PrivateRoute from "../../modules/privateRoute";

and I am using it like this:

<PrivateRoute exact path='/' component={HomePage} />
c-chavez
  • 5,673
  • 4
  • 28
  • 43
nasaa
  • 2,421
  • 7
  • 42
  • 71

1 Answers1

1

Seems like you have not exported HomePage component from its respective file. Please ensure that you have exported the component using export keyword.

class HomePage extends React.Component {
    // your class implementation goes here
}
export default HomePage;

Or, you might not have imported the HomePage component, where you should have imported it before passing to PrivateRoute.

import HomePage from '../../path/to/HomePage';
<PrivateRoute exact path='/' component={HomePage} />
Mohit Gupta
  • 109
  • 1
  • 1
  • 10