Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I try to make my Material-UI RaisedButton link to an external url without success?

As the question in the title state.

Playing with react, react-router and material-ui and in one place I want to make an action button in a Card component link to an external url, like without success.

I'm currently thinking of adding an event handler to use window.open, but it must be a smarter way?

I did find a solution, before the code looked like this.

<CardActions>
   <RaisedButton
       href={this.props.url}
       label="OK"
   />
</CardActions>

The solution was very simple:

<CardActions>
   <a href={this.props.url} target="_blank">
     <RaisedButton
        label="OK"
     />
   </a>
</CardActions>
like image 562
HenrikGr Avatar asked Jul 25 '16 17:07

HenrikGr


3 Answers

If we are adding external link in react-router Link or material-ui Button then This is important we add 'http://' (or https://) in external url. link will not work without add http.

WRONG CODE -

<Link target="_blank" to='www.google.com'>Google</Link>

then redirect link will

localhost:3000/www.google.com

RIGHT CODE -

If we want to redirect with react-router Link then this is example code -

<Link target="_blank" to='http://www.google.com'>Google</Link>

If we want to redirect with material-ui Button then this is example code -

<Button target="_blank" href="http://www.google.com/">Google</Button>

Note: I added target="_blank" in Link/Button. This is optional (and If I will add any external link in my website then I will want to open that link in another Browser Tab not in same Tab.)

like image 93
Arpit Avatar answered Oct 14 '22 04:10

Arpit


You can wrap <RaisedButton /> into <Link /> component for internal routing.

<Link to={this.props.cardItem.resourceUrl}>
  <RaisedButton label="Ok" />
</Link>

And wrap into simple <a> tag for external:

<a href={this.props.cardItem.resourceUrl}>
  <RaisedButton label="Ok" />
</a>
like image 45
Alexandr Lazarev Avatar answered Oct 14 '22 04:10

Alexandr Lazarev


You should use target, component, and href props

<Button                
   target="_blank"
   component="a"
   href="http://www.google.com/"
>
 Google
</Button>
like image 5
Shishir Arora Avatar answered Oct 14 '22 02:10

Shishir Arora