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

What should be the type of the state using react and typescript?

i have code like so,

const [selectedRowId, setSelectedRowId] = React.useState<any | undefined>(
    undefined
); //what should be the type here instead of 'any'


const handleRowClick = React.useCallback(
    row => {
        setSelectedRowId({
            type: row?.original?.type.toLowerCase(),
            id: row?.original?.originalId,
            name: row?.original?.name,
        });
    },
        [setSelectedRowId]
);


return (
    {selectedRowId && (
         <>
             <TabList>
                 <Tab>Controls ({selectedRowId.name})</Tab> //error here
             </TabList>
         </>
     )}
 );

In here what should be the type of selectedRowId instead of ‘any’.

i have tried

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

const [selectedRowId, setSelectedRowId] = React.useState<object | undefined>(
    undefined
);

but this gives error property name doesnt exist on type object on line

Controls ({selectedRowId.name})

could someone help me with this. thanks.

>Solution :

Try to define your object before using the useState and then pass that object to useState. for example in your case:

interface rowType{
      type: String;
      id: number;
       name: Boolean;
    }
    
const [selectedRowId, setSelectedRowId] = React.useState<rowType | undefined>(
  undefined
);
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