Search code examples
reactjsmaterial-uireact-routerreact-router-dom

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>

Solution

  • 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>
        );
    }