简体   繁体   中英

How do I enable on click functionality on a material-ui Icon?

import ShoppingCartIcon from '@material-ui/icons/ShoppingCart'
function App(){
     return (
         <div>
             ShoppingCartIcon></ShoppingCartIcon>
         </div>
      )
}

I want to open an another page when I click on the icon. How do I do it?

First of all, you need to setup a Router in your project. For more information, look into: https://reactrouter.com/web/guides/quick-start . After you are done seting up your routes, you can navigate through the app in many different ways. One of them would be by using the useHistory hook:

import ShoppingCartIcon from '@material-ui/icons/ShoppingCart'

function App(){
     const history = useHistory();

     return (
         <div>
             <ShoppingCartIcon onClick={() => history.push("/page")} />
         </div>
      )
}

Or you can use Link from react router to redirect to another route.

<Link to="/about"><Icon /></Link>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM