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?

  • 1
    https://stackoverflow.com/questions/38340500/export-multiple-classes-in-es6-modules go through the link , its similar to your issue – Kishan Jaiswal Nov 30 '19 at 05: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() ? – eskimo_amigo Nov 30 '19 at 05:32
  • @eskimo_amigo please share your jsbin project – Junius L Nov 30 '19 at 05: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{};` – Crocsx Nov 30 '19 at 06:25

3 Answers3

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
}
Junius L
  • 15,881
  • 6
  • 52
  • 96
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.

junwen-k
  • 3,454
  • 1
  • 15
  • 28
0

You can also do:

export Foo;

export Bar;

in the end instead of declaring the class again.

Junius L
  • 15,881
  • 6
  • 52
  • 96