简体   繁体   English

如何结合 ReactJs Router Link 和 material-ui 组件(如按钮)?

[英]How to combine ReactJs Router Link and material-ui components (like a button)?

I need to find a solution to be able to combine together the functionality of react router with the material ui components.我需要找到一个解决方案,以便能够将 react router 的功能与 material ui 组件结合在一起。

For instance, I've this scenario: a router and a button.例如,我有这个场景:一个路由器和一个按钮。 What I tried to do it is to mix them together, and restyle them.我试图做的是将它们混合在一起,并重新设计它们。

So from a simple link所以从一个简单的链接

<Link className={this.getClass(this.props.type)} to={`${url}`} title={name}>{name}</Link>

I tried to create a material ui button as the following我尝试创建一个材质 ui 按钮如下

<Link className={this.getClass(this.props.type)} to={`${url}`} title={name}>
  <FlatButton label={name} />
</Link>

but I have the following error and Javascript breaks但我有以下错误和 Javascript 中断

invariant.js?4599:38Uncaught Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. invariant.js?4599:38Uncaught Invariant Violation: addComponentAsRefTo(...): 只有 ReactOwner 可以有 refs。 You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies of React loaded (details: https://gist.github.com/jimfb/4faa6cbfb1ef476bd105 ).您可能正在向未在组件的render方法中创建的组件添加 ref,或者您加载了多个 React 副本(详细信息: https : //gist.github.com/jimfb/4faa6cbfb1ef476bd105 )。

Do you have any idea how to manage this situation?您知道如何处理这种情况吗? Thank you in advance and if you need more information let me know预先感谢您,如果您需要更多信息,请告诉我

The way to do in new versions is:在新版本中的做法是:

import { Link } from 'react-router-dom';

// ... some code

render(){
    return (
        <Button component={Link} to={'/my_route'}>My button</Button>
    );
}

Look at this thread or this question看看这个线程或这个问题

This works for me:这对我有用:

<FlatButton label="Details" 
            containerElement={<Link to="/coder-details" />} 
            linkButton={true} />

See https://github.com/callemall/material-ui/issues/850https://github.com/callemall/material-ui/issues/850

<Button
          size="large"
          color="primary"
          onClick={() => {}}
          variant="outlined"
          component={RouterLink}
          to={{
            pathname: `enter your path name`,
          }}
        >
          Click Here
        </Button>

You can try this way when using typescript:使用打字稿时可以尝试这种方式:

import { NavLink as RouterLink } from "react-router-dom";
import {
  Button,
  Collapse,
  ListItem,
  makeStyles,
  ListItemIcon,
  ListItemText,
} from "@material-ui/core";

type NavItemProps = {
  className?: string;
  depth: number;
  href?: string;
  icon?: any;
  info?: any;
  open?: boolean;
  title: string;
};

const NavItem: React.SFC<NavItemProps> = ({

const CustomLink = React.forwardRef((props: any, ref: any) => (
    <NavLink
      {...props}
      style={style}
      to={href}
      exact
      ref={ref}
      activeClassName={classes.active}
    />
  ));
  return (
    <ListItem
      className={clsx(classes.buttonLeaf, `depth-${depth}`)}
      disableGutters
      style={style}
      key={title}
      button
      component={CustomLink}
      {...rest}
    >
      <ListItemIcon>
        {Icon && <Icon className={classes.icon} size="20" />}
      </ListItemIcon>
      <ListItemText primary={title} className={classes.title} />
    </ListItem>
  );
})


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将材料表与 Material-UI 对话框相结合? (反应JS) - How to combine Material-table with Material-UI Dialog? (ReactJS) 如何将Material-UI的快餐栏和输入组件结合在一起进行反应? - How to combine Material-UI's snackbar and input components in react? ReactJS和Material-ui - ReactJS and material-ui React-router-dom 的链接弄乱了 material-ui 的 AppBar 按钮的样式 - React-router-dom's Link messes up styles for material-ui's AppBar Button Material-ui添加来自react-router的Radio Button组件的链接 - Material-ui adding Link to Radio Button component from react-router 在展开一个组件(Material-UI + ReactJS)时,如何保留组件(卡片组件)的位置? - How can I retain the position of components (Card component) while one is expanded (Material-UI + ReactJS)? Material-ui 从 react-router 添加 Link 组件 - Material-ui adding Link component from react-router 如何获得Material-UI的 <TextField/> 用ref =&#39;&#39;正确返回 <input/> 在ReactJS中? - How to get Material-UI's <TextField/> to return correctly with ref='' like <input/> in ReactJS? Material-UI组件的问题 - Problems with Material-UI components ReactJS + Material-UI:如何在每个TableRow中使用Material-UI的FlatButton和Dialog? - ReactJS + Material-UI: How to use Material-UI’s FlatButton and Dialog in each TableRow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM