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

How to rewrite code into one using javascript and react?

i have two methods like below.

 const handleRowClick = React.useMemo(() =>
     !isRunning
         ? (itemId: string) => {
             startRun({items: [itemId] });
         }
         : undefined,
     [startRun]
 );

  const handleSelectedItemsClick = React.useMemo(() =>
      !isRunning
          ? (items: string[]) => startRun({ items: items })
          : undefined,
          startRun]
  );

  handleRowClick(itemId);
  handleSelectedItemsClick(itemIds);

as seen from above handleRowClick takes a string as an argument whereas handleSelectedItemsClick takes an array of strings as an argument.

how can I rewrite the above methods into one such that it can handle either a string or array of strings as input?

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

could someone help me with this? thanks.

>Solution :

at function param set the the type string | string[]

the inside a function

(itemId: string | string[]) => {startRun({items: Array.isArray(itemId)?itemId:[itemId] });}

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