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

186
Vistas
Animating the order of React elements in a list

I'm trying to animate the vertical position of elements within a list using ReactJS. The elements can be arbitrarily ordered within the list, however they all have unique identifiers.

The elements in the list have a static height and only their vertical position changes.

I'm able to apply transform: translate(0px, ${index * height}px) to each element and use transition: transform 1s ease-in-out to animate changes to the translation. This results in the elements being rendered in the appropriate order.

However when the elements are re-ordered, only elements moving "up" are animated; elements which end up further below are "snapped" into place automatically.

Ideally I'd want to do this without the help of a library unless the problem is harder than I think it is to solve.

import shuffle from "lodash.shuffle";
import { useState } from "react";

const data = [
  { id: 1, name: "Foo" },
  { id: 2, name: "Bar" },
  { id: 3, name: "Baz" }
];

export default function App() {
  const [items, setItems] = useState(data);
  return (
    <div>
      <button
        onClick={() => {
          setItems(shuffle(data));
        }}
      >
        Shuffle
      </button>
      <div style={{ position: "relative" }}>
        {items.map(({ id, name }, index) => (
          <div
            key={id}
            style={{
              position: "absolute",
              border: "1px solid #ddd",
              width: "100%",
              height: 42,
              boxSizing: "border-box",
              transition: "transform 1s ease-in-out",
              transform: `translate(${0}px, ${index * 42}px)`
            }}
          >
            {name}
          </div>
        ))}
      </div>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
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