question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BottomNavigationAction with next js not working correctly

See original GitHub issue

Nextjs has a custom Link component https://nextjs.org/docs/api-reference/next/link

With which it manages the links from the server with a <a href=’’> tag for SEO, but from the client as if it were an onclick, in this way you get seo on the server and performance from the client.

To integrate the link of nextjs with material-ui I have been able to do it in the following way.

import Link from "next/link";
import { Button } from "@material-ui/core";
import { List, ListItem, ListItemText } from "@material-ui/core";

<Link href="/about" passHref>
  <Button component="a">About</Button>
</Link>

<List component="nav">
  <Link href="/about" passHref>
   <ListItem button component="a">
      <ListItemText>About</ListItemText>
   </ListItem>
  </Link>
</List>

But when using the Bottom Navigation component of material ui, using the above logic to add the nextjs link, then the links work, but the values stop working, therefore, it no longer detects the page you are on.

<BottomNavigation
  showLabels
  value={value}
  onChange={(event, newValue) => {
    setValue(newValue);
  }}
>
<Link href="/recents" passHref>
  <BottomNavigationAction component="a" label="Recents" value="recents" icon={<RestoreIcon />} />
</Link>
<Link href="/favorites" passHref>
  <BottomNavigationAction component="a" label="Favorites" value="favorites" icon={<FavoriteIcon />} />
</Link>
</BottomNavigation>

The only way I’ve gotten it to work with Link in nextjs is as follows:

import { useRouter } from "next/router";

const router = useRouter();
const onLink = (href) => {
  router.push(href);
};

<BottomNavigation
  showLabels
  value={value}
  onChange={(event, newValue) => {
    setValue(newValue);
  }}
>
<BottomNavigationAction label="Recents" value="recents" icon={<RestoreIcon />}  onClick={() => onLink("/recents")}/>
<BottomNavigationAction label="Favorites" value="favorites" icon={<FavoriteIcon />} onClick={() => onLink("/favorites")}/>
</BottomNavigation>

But this way only works on the client, therefore it generates a problem with SEO, by not rendering the link on the server.

Does anyone have any solutions? An example would be appreciated.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
oliviertassinaricommented, Jun 16, 2021

Duplicate of #26152

Read more comments on GitHub >

github_iconTop Results From Across the Web

BottomNavigationAction with next js not working correctly
To integrate the link of nextjs with material-ui I have been able to do it in the following way. import Link from "next/link";...
Read more >
Using the Material UI Link component with the Next.JS Link ...
So, if I understand correctly -- the above code would function BOTH as a Material-UI Link component AND a NextJS Link component (meaning...
Read more >
Using Next.js Link Component with Material UI Buttons and ...
In this article, I'm going to show you how to properly render material UI buttons and menu items as nextjs links. Rendering a...
Read more >
Navigate Between Pages | Learn Next.js
Learn how to use the Link component to enable client-side navigation between pages. Learn about built-in support for code splitting and prefetching. If...
Read more >
React Material: The definitive guide - LogRocket Blog
Should you use Material Design in your next React project? Find out here to learn how and when to create React apps with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found