Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
¿Cómo enviar datos en el estado setState del estado de tipo matriz?

Tengo data estado. Quería impulsar una nueva entrada del formulario.

 { task:'ANy task',key:Math.random().toString() }

en la matriz de datos mientras usa setData.

Intenté muchas formas mencionadas aquí , pero no sé por qué no funciona.

Aquí está mi código.

 import React, { useState } from "react"; import { StyleSheet, Text, View, TextInput, Button } from "react-native"; import { StatusBar } from "expo-status-bar"; const Addtask = ({navigation}) => { const [data,setData] = useState([]); console.log("from add = ",data) const [task, setTask] = useState(""); const handleSubmit = () => { console.log("submit pressed for task = ", task) const updatedData = [...data,{ task:task, key: Math.random().toString(), }] //here i am setting the data setData(prevState => [...prevState,updatedData]); console.log("data after adding task",data) navigation.navigate("Tasklist",{data:data}) } return ( <View style={styles.container}> <StatusBar style="light" backgroundColor="midnightblue" /> <View> <Text style={styles.text}>Add Task Here</Text> </View> <View> <TextInput style={styles.input} onChangeText={setTask} value={task} onChange={setTask} placeholder="Type your task" keyboardType="ascii-capable" /> </View> <View style={styles.buttoms}> <View style={{margin:4}}> <Button color={'red'} onPress={()=>{navigation.goBack()}} title="Cancel"></Button> </View> <View style={{margin:4}}> <Button color={'lightblue'} onPress={()=>setTask('')} title="Clear"></Button> </View> <View style={{margin:4}}> <Button color={'green'} onPress={handleSubmit} title="Save"></Button> </View> </View> </View> ); }; const styles = StyleSheet.create({ . . . }); export default Addtask;

Para depurar, había usado stmts de la consola que revelan que el valor de la task viene correctamente en handleSubmit() pero no se está presionando.

Tronco

 submit pressed for task = Morning bike ride. data after adding task Array []
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

porque ya tienes

 const updatedData = [...data,{ task:task, key: Math.random().toString(), }]

No necesita setData(prevState => [...prevState,updatedData]) , simplemente puede asignar los datos setData updatedData setData(updatedData)

Puedes usar

 setData(current => [...current, { task:task, key: Math.random().toString(), }])

Entonces no necesitas updatedData

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!