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

326
Views
React Native: ¿Cómo agregar un nuevo elemento en la parte superior de la lista usando Hooks y renderizar usando map ()?

Estoy tratando de agregar nuevos datos y colocarlos en la parte superior de la matriz que obtuve de una API. El problema es que los nuevos datos todavía están al final de la matriz y solo representan los nuevos datos en lugar de todos, aunque console.log genera todos los datos. Sé que el problema no es el estilo porque se pudo representar correctamente si no agregué nuevos datos.

Así es como agrego los nuevos datos:

 fetch() ********** .then((result) => { setListTab(result); setListTab(listTab => [listTab, {id: 18, name: "All", ...listTab}]); console.log(listTab); <ScrollView showsHorizontalScrollIndicator={false} horizontal={true}> {listTab.map(item => ( <TouchableOpacity key={item.id} activeOpacity={1}> <Text>{item.name}</Text> </TouchableOpacity> ))}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Primero, no debe usar ScrollView para implementar listas, use FlatList.

Su lógica (según el código que publicó) no es correcta. Para agregar un nuevo objeto al primero de la matriz, puede usar muy bien el operador de extensión.

Código de ejemplo: https://jsfiddle.net/rzhybtdj/

 let friendsList = [{"id":343,"name":"Satheesh"}, {"id":342,"name":"Kavitha"}]; // something happened and I have a new friend now. // To add it to the firendsList array I do this const newFriend = {"id":341,"name":"Yuvan"}; friendsList = [newFriend, ...friendsList];

Ahora ven a tu código y cómo debería ser para hacer lo que quieres.

 fetch() .then((result) => { const newList = [...result, ...listTab]; //considering result being an array, if an object then [result, ...listTab] setListTab(newList); console.log(newList);

Hazme saber si esto funciona para ti.

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!