Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
TypeError in React Native

I'm following a basic YouTube tutorial and following along I'm getting an error:

'TypeError: Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method.'

I've checked multiple questions and answers, but I still can't get my head around why it's not working or an alternative solution.

export default function App() {

const [task, setTask] = useState();
const [taskItems, setTaskItems] = useState([]);

const handleAddTask = () => {
setTaskItems([...setTaskItems, task])
// console.log(task)
setTask(null)
}

return (
<View style={styles.container}>
  <View style={styles.tasksWrapper}>
    <Text style={styles.sectionTitle}>Today's Tasks</Text>
  

  <View style={styles.items}>
    {
      taskItems.map((item, index) => {
        return (
          <TouchableOpacity key={index}  onPress={() => handleAddTask(index)}>
            <Task text={item} /> 
          </TouchableOpacity>
        )
      })
    }
  </View>
  </View>

  <KeyboardAvoidingView 
    behavior={Platform.OS === "ios" ? "padding" : "height"}
    style={styles.writeTaskWrapper}
    >
      <TextInput style={styles.input} placeholder={'Write a task here'} value={task} onChangeText={text => setTask(text)} />
      <TouchableOpacity onPress={() => handleAddTask()} >
        <View style={styles.addWrapper}>
          <Text style={styles.addText}>+</Text>
        </View>
      </TouchableOpacity>
    </KeyboardAvoidingView>
</View>
);}

The code I'm getting the error with is

setTaskItems([...setTaskItems, task])

Any help would be really appreciated! Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

setTaskItems([...taskItems, task])
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't spread a function and setTaskItems is a function that sets the state of taskItems. Try doing

setTaskItems([...taskItems, task]) 

which I think is what you want to accomplish, to add the task into the existing taskItems array.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda