Harry
Harry

Reputation: 1

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

I am trying to build a next app and am receiving this error. Most the replies I see about this are around not using export default which I am doing!

I am basically trying to create a wrapper around my pages so that I can add things like a nav footer etc.

views/createView:

    const createView: FC = (View: FC) => {
      return (
        <div>
          <View/>
        </div>
      )
    }



export default createView;

pages/idx

import createView from 'views/createView';

const homePage: FC = () => {
  return (
    <div>hi</div>
  )
}

export default createView(homePage);

The code works when I directly return the view but not if I do anything else.

const createView: FC = (View: FC) => View

HELP :(

Upvotes: 0

Views: 8093

Answers (3)

Superior Jacob
Superior Jacob

Reputation: 186

Your instantiating the component and exporting it as a JSX Element, that's why you cant use it like a component because it's an instantiated value, not a function.

Shrinidhi Hegde's answer is correct, simply changing it from <View/> to {View} will solve your problem (because <View/> is attempting to create and render an element and {View} is the act of rendering a created element.).

I would strongly recommend turning your "createView" from a instantiated value to a function that creates a component as it flows nicer in React.

The example below will allow you to continue nicely by simply turning it into a function that creates an 'FC'.

const CreateView = (View: FC): FC => () => (
    <div>
        <View />
    </div>
);

export default CreateView;

Upvotes: 2

Mushfiqur Rahman
Mushfiqur Rahman

Reputation: 564

Remember, component name always be CamelCase and router/page should be small letter. if you are using vscode then you can use this extension extension

and short code for component is rafce

Upvotes: 0

Shri Hegde
Shri Hegde

Reputation: 554

Maybe try

const createView: FC = (View: FC) => {
      return (
        <div>
          {View}
        </div>
      )
    }



export default createView;

Upvotes: 0

Related Questions