Next.js Discord

Discord Forum

Hydration failed

Unanswered
Knospak posted this in #help-forum
Open in Discord
Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <a> in <option>.

How can i fix this problem. I couldn't fix


import Link from "next/link";

const SortProducts = (props) => {
  return (
    <div>
      <select>
        <option value="suggested">
          <Link href="/" prefetch={false}>
            Suggested order
          </Link>
        </option>
        <option value="suggested">
          <Link href="/?s=asc" prefetch={false}>
            Ascending by id
          </Link>
        </option>
        <option value="suggested">
          <Link href="/?s=desc" prefetch={false}>
            Descending by id
          </Link>
        </option>
      </select>
    </div>
  );
};
export default SortProducts;

2 Replies

you can't have an a tag inside an option tag, its not semantically correct and will throw an hydration error
if you want to navigate when changing the option of the select, you can do it programatically with an onChange handler on select and use router.push (https://nextjs.org/docs/app/api-reference/functions/use-router)