Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
array reverse dosen't effect

I try to sort my array to revers and I use the reverse() function but it doesn't affect to view. I must to change the file and save it then I can see the changes.

Items.js:

import { useState } from "react";

const Items = (props) => {
  const [myArray, setMyArray] = useState([
    {
      pId: 1,
      title: "P 1",
    },
    {
      pId: 2,
      title: "P 2",
    },
    {
      pId: 3,
      title: "P 3",
    },
    {
      pId: 4,
      title: "P 4",
    },
  ]);

const handleSort = () => {
    console.log("before", myArray);
    setTimeout(() => {
      setMyArray(myArray.reverse());
      console.log("after", myArray);
    }, 500);
  };

  return (
     <div>
      {myArray.map((a) => (
        <span key={a.pId} className="m-3">
          {a.title}
        </span>
      ))}
       <button onClick={handleSort}>reverse</button>
     </div>
  );

};

export default Items;

enter image description here enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are reversing an array of objects. use the spread operator to spread the elements of your array.

This should work fine.

  let myArray = [
    {
      pId: 1,
      title: "P 1",
    },
    {
      pId: 2,
      title: "P 2",
    },
    {
      pId: 3,
      title: "P 3",
    },
    {
      pId: 4,
      title: "P 4",
    }
  ]
console.log([...myArray].reverse());

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda