React Router V4 Implement NavLink inside a ListItem using Material UI

后端 未结 3 1732
礼貌的吻别
礼貌的吻别 2021-02-06 01:50

I am new to React and I created a simple application with Login and Dashboard page. I have successfully configured my Public Routes

相关标签:
3条回答
  • 2021-02-06 02:15

    You can do it by setting the NavLink as the component of the list item. Here is an example that has worked for me!

    <ListItem
      button
      key="Dashboard"
      component={NavLink} to="/dashboard"
    >
        <ListItemIcon>
            <Dashboard />
        </ListItemIcon>
        <ListItemText primary="Dashboard" />
    </ListItem>
    

    Hope it helps!

    0 讨论(0)
  • 2021-02-06 02:20

    Use the component prop of ListItem. To get active route styling, set activeClassName of NavLink to Mui-selected.

    <ListItem button component={NavLink} to="/" activeClassName="Mui-selected" exact>
      ...
    </ListItem>
    
    0 讨论(0)
  • 2021-02-06 02:28

    You can attach a class to NavLink with something like:

    <NavLink to="/" className="nav-link-item">
      Dashboard
    </NavLink>
    

    Then, attach styling to this class as:

    .nav-link-item {
      color: inherit;
      text-decoration: none;
    }
    
    .nav-link-item:hover,
    .nav-link-item:active,
    .nav-link-item:visited {
      color: red;
      text-decoration: none;
    }
    
    /* Styling for when the link is active */
    .nav-link-item.active {
      color: green;
    }
    
    0 讨论(0)
提交回复
热议问题