【问题标题】:Using Nextjs Link with MUI Link or MUI Button in another (functional) component (types)在另一个(功能性)组件(类型)中使用带有 MUI 链接或 MUI 按钮的 Nextjs 链接
【发布时间】:2021-05-10 19:53:38
【问题描述】:

我将 Next.js (10.2) 和 Material-UI (MUI) 与 Typescript 一起使用,并拥有一个自定义的 Link 组件:

Link.tsx (/components)

import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { withRouter } from 'next/router';
import NextLink from 'next/link';
import MuiLink from '@material-ui/core/Link';

function NextComposed(props) {
  const { as, href, prefetch, forwardedRef, ...other } = props;

  return (
    <NextLink href={href} prefetch={prefetch} as={as}>
      <a {...other} ref={forwardedRef} />
    </NextLink>
  );
}

NextComposed.propTypes = {
  as: PropTypes.string,
  href: PropTypes.string.isRequired,
  prefetch: PropTypes.bool,
  forwardedRef: PropTypes.any.isRequired,
  children: PropTypes.node.isRequired,
}

const NextComposedWithRef = React.forwardRef((props, ref) => <NextComposed {...props} forwardedRef={ref} />)

// A styled version of the Next.js Link component:
// https://nextjs.org/docs/#with-link
function Link(props) {
  const { activeClassName, router, className: classNameProps, naked, ...other } = props;

  const className = clsx(classNameProps, {
    [activeClassName]: router.pathname === props.href && activeClassName,
  });

  if (naked) {
    return <NextComposed className={className} {...other} />;
  }

  return <MuiLink component={NextComposedWithRef} className={className} {...other} />
}

Link.propTypes = {
  activeClassName: PropTypes.string,
  as: PropTypes.string,
  className: PropTypes.string,
  href: PropTypes.string,
  naked: PropTypes.bool,
  onClick: PropTypes.func,
  prefetch: PropTypes.bool,
  router: PropTypes.shape({
    pathname: PropTypes.string.isRequired,
  }).isRequired,
  children: PropTypes.node.isRequired,
};

Link.defaultProps = {
  activeClassName: 'active',
};

export default withRouter(Link)

我也有它的另一个版本,它工作正常。但问题不在于Link 本身,而是当我尝试创建另一个包含我的Link 的component 时,出现以下错误:

CharacterButtons.tsx

type characterButtons {
  name: string
}

const CharacterButtons: FC<characterButtons> = ({ name }) => {
  const classes = useStyles();
  return (
    <Fragment>
      <Grid
        container
        direction="row"
        justify="flex-start"
        alignItems="center"
      >
        <Link href={`https://www.example.com/character/${name}`} prefetch={false}>
          <Avatar variant="square" src={`./test.png`} className={classes.large}/>
        </Link>
      </Grid>
    </Fragment>
  )
};

export default CharacterButtons;

据我了解,它以某种方式与 HTMLPropertyIntrinsicAttributes 类型及其扩展名有关,但我不知道如何。另外,我找到了一些打字方法来避免以下错误,但结果,我需要将父 FC 的每个参数转移到我的 Link,例如:

<Link href={`https://www.example.com/character/${name}`} prefetch={false} ...{parent-props}>

【问题讨论】:

    标签: javascript reactjs typescript next.js


    【解决方案1】:

    我在 GitHub 的这两个 sn-ps 中找到了一个解决方案,我只是在这里重新粘贴代码,但所有学分都归于:herr-vogel (next/link + MUI Button) 和 kachar (next/link + MUI Link)和下一个/链接 + MUI 按钮)

    链接.tsx

    import React, { forwardRef, Ref } from 'react'
    import Link, { LinkProps } from 'next/link'
    import { Link as MuiLink, LinkProps as MuiLinkProps } from '@material-ui/core'
    
    type LinkRef = HTMLAnchorElement
    type NextLinkProps = Omit<MuiLinkProps, 'href' | 'classes'> &
      Pick<LinkProps, 'href' | 'as' | 'prefetch'>
    
    const NextLink = ({ href, as, prefetch, ...props }: LinkProps, ref: Ref<LinkRef>) => (
      <Link href={href} as={as} prefetch={prefetch} passHref>
        <MuiLink ref={ref} {...props} />
      </Link>
    )
    
    export default forwardRef<LinkRef, NextLinkProps>(NextLink)
    

    使用:

    import Link from '../Link';
    

    其中/Link 是带有export default 的tsx 文件

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 2022-11-18
      • 2019-03-21
      • 2012-12-11
      • 1970-01-01
      • 2013-01-24
      • 2015-03-15
      • 2021-04-23
      相关资源
      最近更新 更多