Dev Helpers
← Back

Rotate first item to end

Import and render <RotateFirstItemToEnd /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/RotateFirstItemToEnd.tsx

import { useState } from "react";

export default function RotateFirstItemToEnd({
  entries = ["Beta", "Alpha", "Gamma"],
}: { entries?: string[] } = {}) {
  const [offset, set] = useState(0);
  const n = offset % (entries.length || 1);
  return (
    <button onClick={() => set(offset + 1)}>
      {[...entries.slice(n), ...entries.slice(0, n)].join(" → ") || "Empty queue"}
    </button>
  );
}