最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React. Link MUI without reloading page - Stack Overflow

programmeradmin1浏览0评论

Using ponent MUI Link, my page is reloaded. So I use the Link ponent from react-route-dom, but it turns out that <.a> is inside another and error on console

If I remove MUI.Link, then navbar looks like bad

Code:

import {Link as LinkRouter} from "react-router-dom"
import Link from "@mui/material/Link";

export default function Navbar() {
    return (
        <>
            <GlobalStyles styles={{ul: {margin: 0, padding: 0, listStyle: 'none'}}}/>
            <AppBar
                position="sticky"
                color="default"
                elevation={0}
                sx={{borderBottom: (theme) => `1px solid ${theme.palette.divider}`}}
            >
                <Toolbar sx={{flexWrap: 'wrap'}}>
                    <Typography variant="h4" color="inherit" noWrap sx={{flexGrow: 1}}>
                        <LinkRouter to="/account/wall" className="site-title">YourQuestion</LinkRouter>
                    </Typography>
                    <nav>
                        <CustomLink to="/account/wall">Wall</CustomLink>
                        <CustomLink to="/account/message">Message</CustomLink>
                        <CustomLink to="/account/notice">Notice</CustomLink>
                        <CustomLink to="/account/friends">Friends</CustomLink>
                        <CustomLink to="/account/profile">Profile</CustomLink>
                    </nav>
                </Toolbar>
            </AppBar>
        </>
    )
}

function CustomLink({to, children, ...props}) {
    return (
        <>
            <Link
                variant="button"
                color="text.primary"
                sx={{ my: 1, mx: 1.5 }}
            >
                <LinkRouter to={to} {...props}>
                    {children}
                </LinkRouter>
            </Link>
        </>
    )
}

My github repository

Using ponent MUI Link, my page is reloaded. So I use the Link ponent from react-route-dom, but it turns out that <.a> is inside another and error on console

If I remove MUI.Link, then navbar looks like bad

Code:

import {Link as LinkRouter} from "react-router-dom"
import Link from "@mui/material/Link";

export default function Navbar() {
    return (
        <>
            <GlobalStyles styles={{ul: {margin: 0, padding: 0, listStyle: 'none'}}}/>
            <AppBar
                position="sticky"
                color="default"
                elevation={0}
                sx={{borderBottom: (theme) => `1px solid ${theme.palette.divider}`}}
            >
                <Toolbar sx={{flexWrap: 'wrap'}}>
                    <Typography variant="h4" color="inherit" noWrap sx={{flexGrow: 1}}>
                        <LinkRouter to="/account/wall" className="site-title">YourQuestion</LinkRouter>
                    </Typography>
                    <nav>
                        <CustomLink to="/account/wall">Wall</CustomLink>
                        <CustomLink to="/account/message">Message</CustomLink>
                        <CustomLink to="/account/notice">Notice</CustomLink>
                        <CustomLink to="/account/friends">Friends</CustomLink>
                        <CustomLink to="/account/profile">Profile</CustomLink>
                    </nav>
                </Toolbar>
            </AppBar>
        </>
    )
}

function CustomLink({to, children, ...props}) {
    return (
        <>
            <Link
                variant="button"
                color="text.primary"
                sx={{ my: 1, mx: 1.5 }}
            >
                <LinkRouter to={to} {...props}>
                    {children}
                </LinkRouter>
            </Link>
        </>
    )
}

My github repository

Share Improve this question edited Sep 11, 2022 at 10:41 Ahmet Emre Kilinc 6,98319 gold badges36 silver badges47 bronze badges asked Sep 11, 2022 at 9:32 DanyaTangensDanyaTangens 571 silver badge8 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You can pass ponent="span" prop to MUI Link ponent like this:

<Link
    ponent="span"
    variant="button"
    color="text.primary"
    href="#"
    sx={{ my: 1, mx: 1.5 }}
>
    <LinkRouter to={to} {...props}>
      {children}
    </LinkRouter>
</Link>
import Link from '@mui/material/Link';
import { Link as ReactRouterLink } from 'react-router-dom';

<Link ponent={ReactRouterLink} to="/path">
    Link Text
</Link>

You can use onClick props

      <Link
        sx={{ color: 'primary.main', cursor: 'pointer' }}
        onClick={() => navigate(ROUTES.login)}
        href="#"
      >
        Sign In
      </Link>

More universal variant, based on Maria Odintsova answer:

import { FC } from 'react'
import { Link as ReactRouterLink, LinkProps } from 'react-router-dom'
import Button, { ButtonProps } from '@mui/material/Button'

export const LinkButton: FC<ButtonProps & LinkProps> = ({
  children,
  ...rest
}) => {
  return (
    <Button ponent={ReactRouterLink} {...rest}>
      {children}
    </Button>
  )
}

Usage:

<LinkButton to="/foo">
  Bar
</LinkButton>
发布评论

评论列表(0)

  1. 暂无评论