Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Bootstrap Accordion Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)

I am struggling with my first bootstrap code.

I want to add an Accordion using react-bootstrap code from here.

I think I also already have imported the correct library.

But I still have this error: "Error: Element 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."

Here is my code if anyone can help, thanks a lot ^^ Hugo

import React, { Component } from "react";
import { Accordion } from 'react-bootstrap';
import Card from "react-bootstrap/Card";
import "bootstrap/dist/css/bootstrap.min.css";

export class Maturite extends Component {
  render() {
      return (
        <div>
                  <div style={{marginTop: '150px'}}></div>

                  {/* This is the 1st method from react bootstrap for an Accordion which is NOT working for me  */}
                  <Accordion>
                    <Accordion.Item eventKey="0">
                        <Accordion.Header>Accordion Item #1</Accordion.Header>
                        <Accordion.Body>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        </Accordion.Body>
                    </Accordion.Item>
                    </Accordion>

                    {/* This is a 2nd method for an Accordion which is working for me  */}
                    <Accordion>
                      <Card>
                          <Accordion.Toggle as={Card.Header} eventKey="0">
                              TAB 1
                          </Accordion.Toggle>

                          <Accordion.Collapse eventKey="0">
                              <Card.Body>This is first tab body</Card.Body>
                          </Accordion.Collapse>
                      </Card>

                      <Card>
                          <Accordion.Toggle as={Card.Header} eventKey="1">
                              TAB 2
                          </Accordion.Toggle>

                          <Accordion.Collapse eventKey="1">
                              <Card.Body>This is second tab body</Card.Body>
                          </Accordion.Collapse>
                      </Card>
                  </Accordion>
              </div>
      );
  }
}

export default Maturite;
import React, { Component } from "react";
import { Accordion } from 'react-bootstrap';
import Card from "react-bootstrap/Card";
import "bootstrap/dist/css/bootstrap.min.css";

export class Maturite extends Component {
  render() {
      return (
        <div>
                  <div style={{marginTop: '150px'}}></div>

                  {/* This is the 1st method from react bootstrap for an Accordion which is NOT working for me  */}
                  <Accordion>
                    <Accordion.Item eventKey="0">
                        <Accordion.Header>Accordion Item #1</Accordion.Header>
                        <Accordion.Body>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
                        </Accordion.Body>
                    </Accordion.Item>
                    </Accordion>

                    {/* This is a 2nd method for an Accordion which is working for me  */}
                    <Accordion>
                      <Card>
                          <Accordion.Toggle as={Card.Header} eventKey="0">
                              TAB 1
                          </Accordion.Toggle>

                          <Accordion.Collapse eventKey="0">
                              <Card.Body>This is first tab body</Card.Body>
                          </Accordion.Collapse>
                      </Card>

                      <Card>
                          <Accordion.Toggle as={Card.Header} eventKey="1">
                              TAB 2
                          </Accordion.Toggle>

                          <Accordion.Collapse eventKey="1">
                              <Card.Body>This is second tab body</Card.Body>
                          </Accordion.Collapse>
                      </Card>
                  </Accordion>
              </div>
      );
  }
}

export default Maturite;
like image 958
Hugo Blanadet Avatar asked Jul 19 '21 21:07

Hugo Blanadet


People also ask

How do you fix element type is invalid expected a string for built in components or a class function for composite components but got undefined?

To solve the error "Element type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got", make sure to import named exports using curly braces and default exports without, and only use functions or classes as components.

How do I use Bootstrap accordion in react?

We can use the following approach in ReactJS to use the react-bootstrap Accordion Component. Accordion Props: activeKey: It is the currently active key which corresponds to the expanded card which is currently active. as: It can be used as a custom element type for this component.

What does element type is invalid mean in react?

React: ‘Element type is invalid … check your render method’ Once you’ve seen this error once in your React app you’ll probably instantly recognize it and know exactly what this means: Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

Why is my accordion component not working?

The layout of the component that you're using that doesn't work (using Accordion.Header / .Body) is for the latest beta version of bootstrap ( Bootstrap 5.0 / v2.0.0-beta.4 ). If you're using Bootstrap 4.0 (v1.6.1) then the component has a different format, using Cards (the kind that's working for you).

Why is my `Element type invalid?

`Element 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.

What is uncaught error in react-bootstrap?

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. · Issue #3275 · react-bootstrap/react-bootstrap · GitHub Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.


1 Answers

What version of Bootstrap are you importing?

The layout of the component that you're using that doesn't work (using Accordion.Header / .Body) is for the latest beta version of bootstrap ( Bootstrap 5.0 / v2.0.0-beta.4 ). If you're using Bootstrap 4.0 (v1.6.1) then the component has a different format, using Cards (the kind that's working for you).

like image 154
frejt Avatar answered Oct 02 '22 15:10

frejt