简体   繁体   English

使用 RouterLink 组件反应 MaterialUI 按钮 - 如何传递 Exact 属性?

[英]React MaterialUI Button with RouterLink Component - how to pass Exact property?

I have this code:我有这个代码:

<Button
  activeClassName={classes.active}
  className={classes.button}
  component={RouterLink}
  exact={true}
  to="/app/certificates"
>
  Certificates
</Button>

<Button
  activeClassName={classes.active}
  className={classes.button}
  component={RouterLink}
  exact={true}
  to="/app/certificates/new"
>
  New certificate
</Button>

The thing is - "exact" property is not working - i have "Certificates" button highlighted at "/certificates" page (as it should), but i have BOTH buttons highlighted on "/certificates/new" page because "exact" property not working as it should.问题是 - “exact”属性不起作用 - 我在“/certificates”页面上突出显示了“Certificates”按钮(应该如此),但我在“/certificates/new”页面上突出显示了两个按钮,因为“exact”属性不能正常工作。

How to fix this?如何解决这个问题? Maybe there is the way to pass "exact" property to RouterLink component?也许有办法将“精确”属性传递给 RouterLink 组件?

Thanks!谢谢!

Use Link from react-router-dom instead of a button to navigate.使用 react-router-dom 中的链接而不是按钮进行导航。

npm i react-router-dom

Then change button to Link instead of Button you will have然后将按钮更改为链接而不是您将拥有的按钮

<Link
  activeClassName={classes.active}
  className={classes.button}
  component={RouterLink}
  exact={true}
  to="/app/certificates"
>
  Certificates
</Link>

and

<Link
  activeClassName={classes.active}
  className={classes.button}
  component={RouterLink}
  exact={true}
  to="/app/certificates/new"
>
  New certificate
</Link>

And then the然后

exact精确的

property is passed to the BrowseRouter ie属性被传递给 BrowseRouter 即

<Switch>
  <BrowserRouter path="/app/certificates" exact component={Certificates}/>
  <BrowserRouter path="/app/certificates/new" component={NewCertificate}/>
<Switch/>

Assuming Certificate and NewCertificate are you components names.假设 Certificate 和 NewCertificate 是您的组件名称。

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

相关问题 React MaterialUI - 单击按钮后在 TextField 组件上设置错误 - React MaterialUI - Set error on TextField Component after button is clicked 如何在 React Native 中将函数作为属性传递给组件 - How to pass a function to a component as a property in React Native 如何在反应中将单选按钮的值传递给组件 - How to pass value of radio button to component in react 属性“组件”不存在 reactjs + materialUI + typescript? - Property 'component' does not exist reactjs + materialUI + typescript? 如何使用React.js将状态属性传递给构造函数中的子组件? - How to pass state property to child component in constructor with React.js? 如何将 function 作为属性传递给 React 组件? - How do I pass a function as a property to a React component? Angular 2使用组件属性动态设置routerLink - Angular 2 dynamically set routerLink using a component property React MaterialUI Card:如何在扩展时更改Card的类? -&gt;自定义React组件 - React MaterialUI Card: how to change Card's class on expand? -> Custom React component 如何将按钮组件的标题传递给 React Native 中的函数 - How to pass Button component's title into a function in React Native 如何在 React JS 中单击按钮时在独立组件之间传递数据? - How to pass data between to independent component on button click in React JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM