0

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?

could someone help me with this? thanks.

1
  • 1
    Shouldn't you be using useCallback() if you're memo-ising functions? Commented Feb 11, 2022 at 6:19

2 Answers 2

1

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

the inside a function

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

Sign up to request clarification or add additional context in comments.

Comments

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

const handleRowClick = (itemId) => {
    handleSelectedItemsClick([itemId]);
}

handleRowClick(itemId);
handleSelectedItemsClick(itemIds);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.