Home > Software design >  Typescript - A spread argument must either have a tuple type or be passed to a rest parameter
Typescript - A spread argument must either have a tuple type or be passed to a rest parameter

Time:04-13

I am having some problems with this error:

A spread argument must either have a tuple type or be passed to a rest parameter.

and my code looks like this:

  const [item, setItem] = useState<string>();
  const [items, setItems] = useState([]);


  const handleAddItem = () => {
  console.log(item);
  setItems(...items, item);

}

    <TouchableOpacity onPress={() => handleAddItem()}>
      <View style={styles.addWrapper}>
        <Text style={styles.addText}> </Text>
      </View>
    </TouchableOpacity>

I have searched the other questions posted about this topic but could not seem to fix it with any of those provided. Any ideas and inputs are appreciated!

CodePudding user response:

You need to update the state which is an array; you were very close but you missed the brackets:

setItems([...items, item]);
  • Related