Use NextJs Link with Material UI Link

NextJs + MUI link

Both NextJs and MUI Link component creates an HTML anchor (a) tag when rendering in the browser. So we if we don’t use it correctly we will have a nested a tag in HTML which is not sematically correct.

This is how we should use it.

Import both components from corresponding packages.

// MUI Link
import { Link as MuiLink } from ‘@mui/material’;
// or use default export
// import MuiLink from '@mui/material/Link';

// Next link
import NextLink from 'next/link';

Usage:
Pass the NextLink to the component prop of MUILink

<MuiLink component={NextLink} href="/home">
go to home page
</MuiLink>

The rendered HTML will look like this

Generated HTML

Happy coding 🍺

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kiran Paul V.J.
Kiran Paul V.J.

Written by Kiran Paul V.J.

<engineer type=’computer’ /> <coder /> <maker />

No responses yet

Write a response