Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript React Tabs unable to reslove type errors

I am trying to convert Reactjs Component to Typescript Component

Tabs Component is shown below

<Tabs>
<Tab label="one"></Tab>
<Tab label="two"></Tab>
</Tabs>
import React, { useState } from "react";
const Tabs = ({ children }) => {
  const [activeTab, setActiveTab] = useState(children[0].props.label);
};

I get typescript error like props is not defined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I declare interface to children property?

Code Sandbox Link

>Solution :

You can set the Tabs Props like that:

interface TabsProps {
  children: React.ReactElement<TabProps>[]
};

Thats would "tell" typescript that children are elements that have TabProps, meaning they have label in their props that can be accessed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading