Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Put Routerlink in IconButton in ReactJS

How can i put the routerlink in reactjs button?

I tried these two below: the first one changes the color while the second one, i don't like it since it is reloading the page.

FIRST

<Link to="/settings">
    <IconButton color="inherit">
        <SettingsIcon />
    </IconButton>
</Link>

SECOND

<IconButton color="inherit" href="/settings">
    <SettingsIcon />
 </IconButton>
like image 371
Joseph Avatar asked May 27 '20 05:05

Joseph


People also ask

How do I use Routerlink in react?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.

What is Routerlink in react?

React Router switches from server-side to client-side routing, so when you click on the Link component, the app checks the route and loads the requested component without reloading the full page in the browser.

Can I put a button inside a react router link component?

Any answer here which suggests nesting a html button in a React Router Link component (or vice-versa) will render in a web browser, but it is not semantic, accessible, or valid html: This can lead to layout/styling issues as buttons are not typically placed inside links. Using an html <button> tag with React Router <Link> component.

How do I change the route in react router?

You can change the route using a button with the following code: <button onClick= { () => props.history.push ("path")}> React Router provides some props to your components, including the push () function on history which works pretty much like the < Link to='path' > element.

How to add react-router-Dom in react application?

Follow the steps given below to install Router in your React application: Step 1: cd into your project directory i.e geeks. After installing react-router-dom, add its components to your React application. Adding React Router Components: The main Components of React Router are:

How do I add a link to the menu in react-router-Dom?

To add the link in the menu, use the <NavLink /> component by react-router-dom. The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.


2 Answers

@BeHappy's solution is good, but you can implement this using composition. No need to use hook.

import React from 'react';
import { Link } from 'react-router-dom';
import { IconButton } from '@material-ui/core';
import SettingsIcon from '@material-ui/icons/Settings';

export default function Component() {
  return (
    <IconButton component={Link} to="/setting">
      <SettingsIcon />
    </IconButton>
  );
}
like image 124
piotros Avatar answered Oct 04 '22 03:10

piotros


import React from "react";
import { useHistory } from "react-router-dom";

export default function Component() {
    const history = useHistory();

    return (
        <IconButton color="inherit" onClick={() => history.push("/setting")}>
            <SettingsIcon />
        </IconButton>
    );
}
like image 44
BeHappy Avatar answered Oct 04 '22 03:10

BeHappy