4

I am working on a project in react where I have multiple components. I am trying to do it all in JSBin (lol.. I know). But i am having issues with exporting multiple classes.

class Foo extends React.Component {
    constructor(props) {
        super(props);

        this.state = {

        }
    }

    render() {
        return (
            <div></div>
        );
    }
}


class Bar extends React.Component {
    constructor(props) {
        super(props);

        this.state = {

        }
    }

    render() {
        return (
            <div></div>
        );
    }
}

export class Foo{};
export class Bar{};

But I am getting an error of

  Parsing error: Identifier 'Foo' has already been declared
  92 | }
  93 | 
> 94 | export class Foo{};
     |              ^
  95 | export class Bar{};

So then I tried to change it to this

class Foo extends React.Component {...my code}
class Bar extends React.Component {...my code}

and it compiles but I get an runtime error of

Error: Element type is invalid: expected a string (for built-in components) or a class/function       (for composite components) but got: object.

Is it possible to export multiple classes in a single file with react?

4
  • 1
    stackoverflow.com/questions/38340500/… go through the link , its similar to your issue Commented Nov 30, 2019 at 5:28
  • ^^ I tried those solutions but I think my error stems from my classes extending React.Component and both having constructors with call to super() ? Commented Nov 30, 2019 at 5:32
  • @eskimo_amigo please share your jsbin project Commented Nov 30, 2019 at 5:39
  • did you try just exporting export class Foo extends React.Component and remove those two line where you redeclare a class Foo ? export class Foo{}; Commented Nov 30, 2019 at 6:25

3 Answers 3

3

It's because you are re-declaring the class at the bottom, you can export your classes like the following.

export class Foo extends React.Component {
   // your code
}

export class Bar extends React.Component {
  // your code
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can declare your classes as usual, then export all your classes at the end of the file as the following

class Foo extends React.Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  render() {
    return <div></div>;
  }
}

class Bar extends React.Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  render() {
    return <div></div>;
  }
}

export { Foo, Bar };

Or if you prefer, you can export the class as you declare them.

export class Foo extends React.Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  render() {
    return <div></div>;
  }
}

export class Bar extends React.Component {
  constructor(props) {
    super(props);

    this.state = {};
  }

  render() {
    return <div></div>;
  }
}

Generally its suggested that only export one component per file as per es-lint's rule.

Comments

0

You can also do:

export Foo;

export Bar;

in the end instead of declaring the class again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.