0

In App.tsx, if you hover the <Accordion/>, you will see the error below.

'Accordion' cannot be used as a JSX component.
  Its return type 'Element[]' is not a valid JSX element.
    Type 'Element[]' is missing the following properties from type 'ReactElement<any, any>': type, props, keyts(2786)

How can I fix this problem?

App.tsx

import "./styles.css";

import Accordion from "./Accordion";

export default function App() {
  const items = [
    { id: 1, title: "title 1", description: "description 1" },
    { id: 2, title: "title 2", description: "description 2" }
  ];
  return (
    <div className="App">
      <Accordion items={items} />
    </div>
  );
}

Accordion.tsx

import React from "react";
import {
  Accordion,
  AccordionItem,
  AccordionButton,
  AccordionPanel,
  AccordionIcon,
  Box
} from "@chakra-ui/react";

type AccordionProps = {
  items: Array<{ id: number; title: string; description: string }>;
};

export default function AccordionList({ items }: AccordionProps) {
  return items.map((item) => {
    return (
      <Accordion defaultIndex={[0]} allowMultiple>
        <AccordionItem>
          <AccordionButton>
            <Box flex="1" textAlign="left">
              {item.title}
            </Box>
            <AccordionIcon />
          </AccordionButton>
          <AccordionPanel>{item.description}</AccordionPanel>
        </AccordionItem>
      </Accordion>
    );
  });
}

Codesandbox
https://codesandbox.io/s/determined-ives-lob9x?file=/src/Accordion.tsx:0-734

CCCC
  • 5,665
  • 4
  • 41
  • 88
  • Does this answer your question? [Type '(props: Props) => Element\[\]' is not assignable to type 'FunctionComponent'](https://stackoverflow.com/questions/57651621/type-props-props-element-is-not-assignable-to-type-functioncomponent) – Matt U Jan 03 '22 at 03:59

3 Answers3

1

You should map items inside the Accordion like this

export default function AccordionList({ items }: AccordionProps) {
  return (
    <Accordion defaultIndex={[0]} allowMultiple>
      {items.map((item) => (
        <AccordionItem>
          <AccordionButton>
            <Box flex="1" textAlign="left">
              {item.title}
            </Box>
            <AccordionIcon />
          </AccordionButton>
          <AccordionPanel>{item.description}</AccordionPanel>
        </AccordionItem>
      ))}
    </Accordion>
  );
}

Sanbox https://codesandbox.io/s/patient-bush-167ye?file=/src/Accordion.tsx:250-727

Dhia Djobbi
  • 1,176
  • 2
  • 15
  • 35
Akshay
  • 14,138
  • 5
  • 46
  • 70
0

import AccordionList from "./Accordion";

to import , need export

baeksm
  • 71
  • 3
0
import React from "react";
import {
Accordion,
AccordionItem,
AccordionButton,
AccordionPanel,
AccordionIcon,
Box
} from "@chakra-ui/react";

type AccordionProps = {
items: Array<{ id: number; title: string; description: string }>;
};

export default function AccordionList({ items }: AccordionProps) {
return(<> 
{items.map((item, idx) => {
return (
  <Accordion key = {idx} defaultIndex={[0]} allowMultiple>
    <AccordionItem>
      <AccordionButton>
        <Box flex="1" textAlign="left">
          {item.title}
        </Box>
        <AccordionIcon />
      </AccordionButton>
      <AccordionPanel>{item.description}</AccordionPanel>
    </AccordionItem>
  </Accordion>
);
})
}
</>);
}

Change your accordion code to the above one. You have encountered this error because you are sending more than one JSX element from a function. Wrapping it in a fragment is one of the solutions to address this issue.

lokprakash
  • 420
  • 3
  • 9