0

In my project, I am trying to insert multiple values ​​at the same time through a text field. Added items and setItems for that. But now there is an error " TS2345: Argument of type '(i: string) => any[]' cannot be assigned to parameter of type 'string[]'". I'm a beginner in react typescript, so any corrections when passing items? Please give me some suggestions to fix this problem.

email.tsx

const Email = (props: Props) => {
  const [items, setItem] = useState<string[]>([]);
...
...
 <EmailChip
            placeholder="Enter Email Addresses"
            LabelName="Enter Email Addresses"
            className="textfield"
            tooltip="true"
            upload
            items={items}
            setItem={setItem}
          />

emailpage.tsx

type Props = {
  ...
  items?: string | any;
  setItem: (items: string[]) => void;
  ...
  const handlePaste = (evt: any) => {
    evt.preventDefault();

    const paste = evt.clipboardData.getData("text");
    const emails = paste.match(/[\w\d\\.-]+@[\w\d\\.-]+\.[\w\d\\.-]+/g);

    if (emails) {
      const toBeAdded = emails.filter((email: any) => !isInList(email));

      

      setItem((i: string) => [...i, ...toBeAdded]);
      console.log("items after", items);
    }
  };
  ...

};

error

enter image description here

6
  • In which line do you get this error? Commented Sep 19, 2022 at 13:54
  • Why does your your type declares setItem two times? Commented Sep 19, 2022 at 13:55
  • question edited and added the error screen too.. Commented Sep 19, 2022 at 13:58
  • You are trying to pass a new function to setItem but setItem allows only array of string as an argument Commented Sep 19, 2022 at 13:59
  • Can you suggest how to rearrange that line? Commented Sep 19, 2022 at 14:01

1 Answer 1

1

You probably wanted to declare setItem as:

setItem: React.Dispatch<React.SetStateAction<string[]>>

And use it like this:

setItem((i: string[]) => [...i, ...toBeAdded])
Sign up to request clarification or add additional context in comments.

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.