Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The default export is not a React Component in page: "/" NextJS

Not sure why the error is coming up, heres my index.js and App.js (the default export). I have used export default in the app.js.

index.js:

import "./index.css";
import App from "./App";
import "bootstrap/dist/css/bootstrap.css";
import "font-awesome/css/font-awesome.css";
<App />;

App.js

import React, { Component } from "react";
import { ToastContainer } from "react-toastify";
import NavBar from "../components/navBar";
import auth from "../services/authService";
import "react-toastify/dist/ReactToastify.css";
import "./App.css";

class App extends Component {
  state = {};

  componentDidMount() {
    const user = auth.getCurrentUser();
    this.setState({ user });
  }

  render() {
    const { user } = this.state;

    return (
      <React.Fragment>
        <ToastContainer />
        <NavBar user={user} />
      </React.Fragment>
    );
  }
}

export default App;
like image 586
Keytoll Avatar asked Jan 23 '20 07:01

Keytoll


3 Answers

Date: 23/01/2021

I've also faced this issue on my Next.js app.

If you're using a functional component instead of a class component you'll get this error as well in some casses. Exactly I don't know why this is happening but I just resolved this issue by exporting my component at the bottom of the page.

Like this,

Case Error scenario:

export default function Home(){
      return (<>some stuffs</>)
}

Home.getInitialProps = async (ctx) => {
    return {}
}
Error: The default export is not a React Component in page: "/"

How to resolved it?

I just put my export at the bottom my page and then it'll be gone.

Like this,

function Home(){
      return (<>some stuffs</>)
}

Home.getInitialProps = async (ctx) => {
    return {}
}

export default Home;

Hopefully, it'll be helpful for you!

like image 59
Mohamed Jakkariya Avatar answered Oct 05 '22 12:10

Mohamed Jakkariya


Fixed, made index.js a class and exported it.

import React, { Component } from "react";
import "./index.css";
import App from "./App";
import "bootstrap/dist/css/bootstrap.css";
import "font-awesome/css/font-awesome.css";
class Index extends Component {
  state = {};
  render() {
    return <App />;
  }
}

export default Index;
like image 29
Keytoll Avatar answered Oct 05 '22 13:10

Keytoll


In next.js all pages has to be exported default. Either this way

export default function RegisterPage() {
  return (
    <>
     
    </>
  );
}

or

const Home: NextPage = () => {
  return (
    <BaseLayout>
    
    </BaseLayout>
  );
};

export default Home;

If not you get that error.

like image 44
Yilmaz Avatar answered Oct 05 '22 11:10

Yilmaz