Dev Helpers
← Back

Jump menu with submit

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

Open this example in the interactive library

Live preview

Source code

// components/JumpMenuWithSubmit.tsx

import { useState } from "react";

export default function JumpMenuWithSubmit({
  links = [
    { label: "Guide", href: "/guide" },
    { label: "About", href: "/about" },
  ],
}: { links?: { label: string; href: string }[] } = {}) {
  const [href, set] = useState(links[0]?.href ?? "");
  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        if (links.some((l) => l.href === href)) location.assign(href);
      }}
    >
      <label>
        Go to{" "}
        <select value={href} onChange={(e) => set(e.target.value)}>
          {links.map((l) => (
            <option key={l.href} value={l.href}>
              {l.label}
            </option>
          ))}
        </select>
      </label>
      <button disabled={!href}>Go</button>
    </form>
  );
}