Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a NavLink to a Material UI component via the containerElement prop gives “Failed prop type” warning

<RaisedButton containerElement={NavLink} to="/somewhere">
   Somewhere
</RaisedButton>

Produces the following warning:

Warning: Failed prop type: Invalid prop `containerElement` supplied to `RaisedButton`.
in RaisedButton (at App.js:11)
in App (at index.js:23)
in Provider (at index.js:22)
in MuiThemeProvider (at index.js:21)

but the Navlink properly renders and redirects to /somewhere on click. If this is a deprecated prop that still works then I haven't been able to find what the new prop is called... Please advise. If nothing else I'd like to hide the warning (how?).

like image 669
Dockson Avatar asked Sep 20 '17 20:09

Dockson


People also ask

When should I use NavLink?

The NavLink is used when you want to highlight a link as active. So, on every routing to a page, the link is highlighted according to the activeClassName . Link is for links that need no highlighting. And a is for external links.

What is NavLink tag?

A special version of the <Link> that will add styling attributes to the rendered element when it matches the current URL.


2 Answers

As of Material UI 1.0 the prop is called component:

import { Link } from 'react-router-dom'
import Button from 'material-ui/Button';

<Button component={Link} to="/open-collective">
  Link
</Button>

More about Buttons.

Update:

From Material UI v4 you may need to use forwardRef and wrap Link into a div:

const LinkRef = React.forwardRef((props, ref) => <div ref={ref}><Link {...props} /></div>)

<Button component={LinkRef} to="/open-collective">
  Link
</Button>

More here.

like image 63
haxpanel Avatar answered Jun 12 '23 02:06

haxpanel


I think you need to give it the markup for the containerElement, something like this (rather than just the name of the element)

containerElement={<NavLink to="/home" />}

Give that a try and see if it works

like image 40
Mikkel Avatar answered Jun 12 '23 01:06

Mikkel