Next.js Discord

Discord Forum

Can't we have div inside a Link component?

Answered
Anbu posted this in #help-forum
Open in Discord
I receive this error.

const PathCardList: React.FunctionComponent<IPathCardListProps> = ({
  href,
  type,
  title,
  desc,
  interview,
  quiz,
}) => {
  return (
    <Link
      href={href}
      className="bg-white p-10 rounded shadow flex items-center space-x-10"
    >
      <div className="w-36 h-36 rounded bg-rose-300 flex items-center justify-center font-semibold text-xs">
        {type}
      </div>
      <div className="space-y-3">
        <h4 className="font-semibold">{title}</h4>
        <p className="text-gray-600">{desc}</p>
        <div className="space-x-4">
          <Link
            href={interview}
            className="px-4 py-2 rounded bg-indigo-500 hover:bg-indigo-600 text-white text-xs font-semibold uppercase"
          >
            Interview Questions
          </Link>
          <Link
            href={quiz}
            className="px-4 py-2 rounded bg-green-500 hover:bg-green-600 text-white text-xs font-semibold uppercase"
          >
            Practice
          </Link>
        </div>
      </div>
    </Link>
  );
};
Answered by joulev
This problem happens everywhere. In other frameworks it is a hidden bug, the browser implicitly puts the two links as siblings (read the article above) because they always try to correct invalid html. In react’s case due to its hydration mechanism it becomes a visible bug with a visible error message
View full answer

8 Replies

Yes you are right. but i have a condition like that? What do you suggest?
You should probably redesign the component. I would only make the title a link, not the entire card.

If you absolutely need to have the entire card being a link, use relative/absolute positioning with css to make the buttons look like they are inside the link but is actually outside it
https://css-tricks.com/nested-links/ read the comment section
Looks like the above is kind of work-around. In VueJS, i think, we don' t have this kind of problem.. But thanks. helped me to debug.
This problem happens everywhere. In other frameworks it is a hidden bug, the browser implicitly puts the two links as siblings (read the article above) because they always try to correct invalid html. In react’s case due to its hydration mechanism it becomes a visible bug with a visible error message
Answer
Thanks for the explanation.
Barbary Lion
The problem is you have a <Link> inside of a <Link> and you cant have an <a> tag nested in <a> its not valid HTML so it doesnt encapsulate the same way. its not a hidden bug write valid HTML lol