Material-ui: react-router์™€ ํ˜ธํ™˜๋˜๋Š” ListItem ๋ณ€ํ™˜

์— ๋งŒ๋“  2017๋…„ 08์›” 30์ผ  ยท  21์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: mui-org/material-ui

List ๊ฐ ListItem ์„ Link ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋งŒ๋“ค๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

Drawer ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ List ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•ด๋‹น ๋ชฉ๋ก์€ 4 ๊ฐœ์˜ ListItem ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.
๋‚ด๊ฐ€ ๋‹ฌ์„ฑํ•˜๊ณ  ์‹ถ์€ ๊ฒƒ์€ ๊ฐ ListItem ์„ React ๋ผ์šฐํ„ฐ Link ์ž…๋‹ˆ๋‹ค.
์„œ๋ž์˜ onClick ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ํด๋ฆญ ํ•œ ListItem์˜ Id๋ฅผ ์–ป์€ ๋‹ค์Œ ID๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ ์ฐฝ์˜ ์œ„์น˜๋ฅผ โ€‹โ€‹๋ณ€๊ฒฝํ•˜๋ ค๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ๋‚˜๋Š”์ด ์ ‘๊ทผ๋ฒ•์ด ๋ฐ˜์‘ ๊ฐœ๋…๋ณด๋‹ค ๋” jqueryish๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  Link ์— component ์— ์†Œํ’ˆ ListItem ,ํ•˜์ง€๋งŒ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. (๋‚˜๋Š” ๋‹น์‹ ์ด์—์„œ ์‚ฌ์šฉํ•œ ๋ณธ ์ ์ด component ์— ์†Œํ’ˆ ๋ฌธ์„œ).
์ด ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•˜๋Š” ๊ถŒ์žฅ ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Drawer from 'material-ui/Drawer';
import List, { ListItem, ListItemIcon, ListItemText } from 'material-ui/List';
import Face from 'material-ui-icons/Face';
import Person from 'material-ui-icons/Person';
import Assignment from 'material-ui-icons/Assignment';
import Link from 'react-router-dom/Link';
import { FormattedMessage } from 'react-intl';

const styles = {
   list: {
      width: 250,
      flex: 'initial',
    },
};

 class UndockedDrawer extends Component {

   constructor() {
      super();
      this.onClick = this.onClick.bind(this);
    }

    onClick(e, data) {
       console.log(data);
       this.props.onRequestClose();
     }

   render() {
       const classes = this.props.classes;
        const sidebarListItems = (
     <div>
         <ListItem button >
            <ListItemIcon>
               <Person />
           </ListItemIcon>
            <ListItemText primary={<FormattedMessage id="user" />} />
          </ListItem>
    <ListItem button>
      <ListItemIcon>
        <Assignment />
      </ListItemIcon>
      <ListItemText primary={<FormattedMessage id="consultant" />} />
    </ListItem>
    <ListItem button>
      <ListItemIcon>
        <Face />
      </ListItemIcon>
      <ListItemText primary={<FormattedMessage id="student" />} />
    </ListItem>
  </div>
);

const sidebarList = (
  <div>
    <List className={classes.list} >
      {sidebarListItems}
    </List>
  </div>
);

return (
  <div>
    <Drawer
      anchor="right"
      open={this.props.open}
      onRequestClose={this.props.onRequestClose}
      onClick={this.onClick}
    >
      {sidebarList}
    </Drawer>
   </div>
   );
   }
 }

  UndockedDrawer.propTypes = {
     classes: PropTypes.shape({}).isRequired,
     open: PropTypes.bool.isRequired,
     onRequestClose: PropTypes.func.isRequired,
    };

   export default withStyles(styles)(UndockedDrawer);
  • Material-UI : 1.0.6- ๋ฒ ํƒ€
  • ๋ฐ˜์‘ : ^ 15.6.1
  • ๋ธŒ๋ผ์šฐ์ € : ํฌ๋กฌ : 60
  • react-router-dom : ^ 4.1.2
question

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์ด๋Š” https://material-ui.com/components/buttons/#third -party-routing-library ๋ฌธ์„œ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

import React from 'react';
import { MemoryRouter as Router } from 'react-router';
import { Link, LinkProps } from 'react-router-dom';
import ListItem from '@material-ui/core/ListItem';
import { Omit } from '@material-ui/types';

// The usage of React.forwardRef will no longer be required for react-router-dom v6.
// see https://github.com/ReactTraining/react-router/issues/6056
const AdapterLink = React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => (
  <Link innerRef={ref as any} {...props} />
));

const CollisionLink = React.forwardRef<HTMLAnchorElement, Omit<LinkProps, 'innerRef' | 'to'>>(
  (props, ref) => <Link innerRef={ref as any} to="/getting-started/installation/" {...props} />,
);

export default function ButtonRouter() {
  return (
    <Router>
      <ListItem color="primary" component={AdapterLink} to="/">
        Simple case
      </ListItem>
      <ListItem component={CollisionLink}>Avoids props collision</ListItem>
    </Router>
  );
}

๋ชจ๋“  21 ๋Œ“๊ธ€

๋‹ค์Œ๊ณผ ๊ฐ™์ด Link ListItem ์„ (๋ฅผ) ๋ž˜ํ•‘ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<Link to="users" style={{ textDecoration: 'none' }}>
      <ListItem button >
        <ListItemIcon>
          <Person />
        </ListItemIcon>
        <ListItemText primary={<FormattedMessage id="user" />} />
      </ListItem>
    </Link>

์ด ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด onClick ๋ฉ”์„œ๋“œ๋„ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
์ œ์•ˆ์ด๋‚˜ ๊ฐœ์„  ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๊นŒ?

๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด Link์— ListItem์„ ๋ž˜ํ•‘ ํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ์•˜์Šต๋‹ˆ๋‹ค.

์ด ์†”๋ฃจ์…˜์€ ์˜๋ฏธ ์ƒ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋ฌธ์„œ์—์„œ ๋ฌธ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์‹ญ์‹œ์˜ค.

https://github.com/callemall/material-ui/blob/92bd073015b9cc0dff3a26195fcb49153ddaab78/docs/src/modules/components/AppDrawerNavItem.js#L71 -L83

๋‹น์‹ ์€ ๋˜ํ•œ ์ด๊ฒƒ์„ ์‹œ๋„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

https://github.com/callemall/material-ui/blob/92bd073015b9cc0dff3a26195fcb49153ddaab78/docs/src/pages/demos/buttons/FlatButtons.js#L40 -L42

์ด๋Š” https://material-ui.com/components/buttons/#third -party-routing-library ๋ฌธ์„œ์—์„œ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

import React from 'react';
import { MemoryRouter as Router } from 'react-router';
import { Link, LinkProps } from 'react-router-dom';
import ListItem from '@material-ui/core/ListItem';
import { Omit } from '@material-ui/types';

// The usage of React.forwardRef will no longer be required for react-router-dom v6.
// see https://github.com/ReactTraining/react-router/issues/6056
const AdapterLink = React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => (
  <Link innerRef={ref as any} {...props} />
));

const CollisionLink = React.forwardRef<HTMLAnchorElement, Omit<LinkProps, 'innerRef' | 'to'>>(
  (props, ref) => <Link innerRef={ref as any} to="/getting-started/installation/" {...props} />,
);

export default function ButtonRouter() {
  return (
    <Router>
      <ListItem color="primary" component={AdapterLink} to="/">
        Simple case
      </ListItem>
      <ListItem component={CollisionLink}>Avoids props collision</ListItem>
    </Router>
  );
}

๋ฌธ์„œ์—์„œ ๋ณผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
https://material-ui-next.com/demos/lists/
https://material-ui-next.com/demos/drawers/

๋ผ์šฐํ„ฐ ๋ฐ˜์‘ ์‚ฌ์šฉํ•  ๋•Œ ์ผ๋ฐ˜์ ์ธ ์ผ์ด์ง€๋งŒ ๊ทธ๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ถ„๋ช…ํ•˜์ง€ ์•Š๋‹ค NavLink A์˜ ListItem .
component ์†Œํ’ˆ์ด์žˆ๋Š” Button ?
๋ฒ„ํŠผ ์†Œํ’ˆ๊ณผ ๊ตฌ์„ฑ ์š”์†Œ ์†Œํ’ˆ์ด ๋™์ผํ•œ JSX ํƒœ๊ทธ์— ํ•จ๊ป˜ ํ˜ผํ•ฉ๋˜์–ด ์žˆ์Šต๋‹ˆ๊นŒ?
์ด๋Ÿฐ ์ข…๋ฅ˜์˜ ๋ฏน์Šค ์ธ์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?
์™œ ๊ทธ๋ ‡๊ฒŒ ๋ณต์žกํ•œ๊ฐ€์š”?

@mehrdaad ์˜ ์†”๋ฃจ์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ ๋งํฌ๋Š” ์ผ๋ฐ˜ MUI ๋ชฉ๋ก์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ @oliviertassinari๊ฐ€ ์ œ์•ˆํ•œ๋Œ€๋กœ component={Link} ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šคํƒ€์ผ์ด ์—‰๋ง์ด๋ฉ๋‹ˆ๋‹ค.
(์ด๋ฏธ์ง€์—์„œ ๋งˆ์šฐ์Šค๋Š” "Build"์œ„์— ์žˆ์ง€๋งŒ ๋‘ ๋ฒˆ์งธ ํ•ญ๋ชฉ์—๋Š” ๋ฐ‘์ค„์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.)

screen shot 2017-11-16 at 12 09 05

@ilyador ์ด UI ๋งํฌ ์Šคํƒ€์ผ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

@ zhangwei900808 ์•„๋‹ˆ์š”, ์—ฌ์ „ํžˆ ์˜๋ฏธ ์ƒ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค. <ul><a ..>... ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
https://codesandbox.io/s/lpwq74p30m

์œ„์— ์ œ๊ณต๋œ ๋‹ต๋ณ€์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

@oliviertassinari ๋” ์‰ฌ์šด ๋ฐฉ๋ฒ•์ด

const styles = {
    li: {
        '&:hover': {
            backgroundColor: 'transparent',
        },
    },
};
// ...
<MenuItem disableGutters className={classes.li}>
    <Button component={Link} to="/logout" ...

์ด์ค‘ ํ˜ธ๋ฒ„ ํšจ๊ณผ๋ฅผ ๋ฐฉ์ง€ํ•˜๋ ค๋ฉด MenuItem ๋ฐ Button (์—ฌ์ „ํžˆ ์•ฝ๊ฐ„์˜ ๊ฑฐํ„ฐ / ํŒจ๋”ฉ์ด ์žˆ์Œ, ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ)
๋ฒˆ๊ฑฐ๋กญ๊ธฐ ๋•Œ๋ฌธ์—

๋ฐ๋ชจ : https://codesandbox.io/s/nk834yk5pl (์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” component={Link} to="/..." ํ•˜์ง€๋งŒ ๋™์ผํ•ฉ๋‹ˆ๋‹ค)

@caub ์ถ”๊ฐ€ Button ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋„์ž…ํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? MenuItem์—์„œ ๊ตฌ์„ฑ ์š”์†Œ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@oliviertassinari ์™œ๋ƒํ•˜๋ฉด ul > a ์€ ์œ ํšจํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. html5 https://codesandbox.io/s/lpwq74p30m

@caub nav > a ์€ (๋Š”) ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.

์ข‹์•„, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. https://codesandbox.io/s/lpwq74p30m

๋‚ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•œ ํ•ด๊ฒฐ์ฑ…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<List>
        {menus.map((menu: any, index: any) => {
          return (
            <ListItem
              key={index}
              {...{ to: menu.link }}
              component={Link}
              button={true}
            >
              <ListItemIcon>{menu.icon}</ListItemIcon>
              <ListItemText primary={menu.text} />
            </ListItem>
          );
        })}
      </List>

๋”ฐ๋ผ์„œ ListItem์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.
key = {index}
** {... {to : menu.link}} **
component = {Link}
button = {true}
>

import {Link} from 'react-router-dom'

<ListItem component={Link} to="/" button>

// or
<ListItem component={props => <Link to="/" {...props} />} button>

ํŠธ์œ— ๋‹ด์•„ ๊ฐ€๊ธฐ

์ด๊ฒƒ์€ ๋‚˜์—๊ฒŒ ์™„๋ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌ!

MUI v4 ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ (4.3.1์— ์žˆ์Œ) ์ดํ›„ ๊ฐ€์žฅ ๋งŽ์ด ๋“ํ‘œ ํ•œ ๋‹ต๋ณ€์€ "ํ•จ์ˆ˜ ๊ตฌ์„ฑ ์š”์†Œ์— ์ฐธ์กฐ๋ฅผ ์ œ๊ณต ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.์ด ์ฐธ์กฐ์— ์•ก์„ธ์Šคํ•˜๋ ค๋Š” ์‹œ๋„๊ฐ€ ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. React.forwardRef ()๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๊นŒ?"๋กœ ์ธํ•ด ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

NavLink ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ž˜ํผ๋กœ ๊ต์ฒดํ–ˆ์Šต๋‹ˆ๋‹ค.

import React, { Component } from "react"
import { NavLink } from "react-router-dom"

/**
 * React Router Nav Link wrapper to forward the ref to fix "Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?"
 *
 * From https://material-ui.com/guides/composition/#caveat-with-refs
 */
class NavLinkMui extends Component {
    render() {
        const { forwardedRef, ...props } = this.props
        return <NavLink {...props} ref={forwardedRef} />
    }
}

export default NavLinkMui

์šฉ๋ฒ•:

<ListItem
    button
    component={NavLinkMui}
    to='/'
>
    <ListItemIcon>
        <SlideshowIcon />
    </ListItemIcon>
</ListItem>

@HugoGresse ์˜ˆ, forward ref API๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ์—์„œ ์˜ˆ์ œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. https://github.com/mui-org/material-ui/issues/7956#issuecomment -326908167์„ ์ตœ์‹  ๋‹ต๋ณ€์œผ๋กœ ์—…๋ฐ์ดํŠธํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ดœ์ฐฎ์€์ง€ ์•Œ๋ ค์ฃผ์„ธ์š”.

@oliviertassinari ๊ทธ๋žฌ ์Šต๋‹ˆ๋‹ค.

Button ํ…์ŠคํŠธ ์ƒ‰์ƒ์ด ํ…Œ๋งˆ ํŒ”๋ ˆํŠธ ์ƒ‰์ƒ์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š” forwardRef ์†”๋ฃจ์…˜ ๋˜๋Š” Component 1์— ์—ฌ์ „ํžˆ ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ์ƒ‰์ƒ์€ palette.type: 'dark' ์™€ ์ผ์น˜ํ•˜๋„๋ก ํฐ์ƒ‰์ด์–ด์•ผํ•˜์ง€๋งŒ ํฐ์ƒ‰์ž…๋‹ˆ๋‹ค.

component ์†Œํ’ˆ ์—†์ด๋„ ์—ฌ์ „ํžˆ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— MUI์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ๊ณผ ์—ฐ๊ฒฐ๋˜์—ˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Capture d'eฬcran 2019-08-06 16 54 51

์—ฌ๊ธฐ์—์„œ ์ฝ๋Š” ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด ์ถ”๊ฐ€ ์†Œํ’ˆ์„ ListItemText์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<ListItemText
    primaryTypographyProps={{
        color: 'textPrimary'
    }}
    primary="Dashboard"
/>

BTW, ๊ท€ํ•˜์˜ ์˜ˆ๋Š” ์ด์ œ TS์— ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•œ ํ•ด๊ฒฐ์ฑ…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<List>
        {menus.map((menu: any, index: any) => {
          return (
            <ListItem
              key={index}
              {...{ to: menu.link }}
              component={Link}
              button={true}
            >
              <ListItemIcon>{menu.icon}</ListItemIcon>
              <ListItemText primary={menu.text} />
            </ListItem>
          );
        })}
      </List>

๋”ฐ๋ผ์„œ ListItem์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.
key = {index}
** {... {to : menu.link}} **
component = {Link}
button = {true}
>

button={true} ๋‹จ์ˆœํžˆ ์„œ๋ž์˜ UI ๋™์ž‘์„ ๋งํฌ๊ฐ€์—†๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

Typescript์—์„œ ์ด๊ฒƒ์„ ์‹œ๋„ํ•˜๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์„ ์œ„ํ•ด :

ListItem ๊ตฌ์„ฑ ์š”์†Œ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ์š”์†Œ๋ฅผ ListItem ๊ตฌํ˜„์œผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Typescript์˜ ํŠธ๋ฆญ์€ ListItem์˜ ์†Œํ’ˆ์— ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž (...)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์ด ์œ ํ˜•์˜ ๊ตฌ์„ฑ ์š”์†Œ ๋ฐ ListItem์— ๋Œ€ํ•œ "to"์†์„ฑ์„ ์ง€์ •ํ•˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ (Typescript ์˜ค๋ฅ˜ TS2769 ์†์„ฑ '๊ตฌ์„ฑ ์š”์†Œ'๊ฐ€ 'IntrinsicAttributes ...'์œ ํ˜•์— ์—†์Šต๋‹ˆ๋‹ค.

import { NavLink } from "react-router-dom";

        <List>
            <ListItem button={true} {...{ component: NavLink, to: "/Somewhere" }}>
                <ListItemIcon>
                    <ShoppingCartIcon />
                </ListItemIcon>
                <ListItemText primary="Orders" />
            </ListItem>
        </List>

๋˜๋Š” ์˜ˆ๋ฅผ ๋“ค์–ด ListItem์—์„œ onClick ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ :


    <List>
        <ListItem button={true} {...{ onClick: () => alert("foo") }}>
            <ListItemIcon>
                <DashboardIcon />
            </ListItemIcon>
            <ListItemText primary="Dashboard" />
        </ListItem>
    </List>

```

์ด๊ฒƒ์€ ๋‚˜๋ฅผ ์œ„ํ•ด ์ผํ–ˆ์Šต๋‹ˆ๋‹ค.

const NavLinkMui = React.forwardRef((props, ref) => (
  <NavLink {...props} activeClassName="Mui-selected" ref={ref} />
))
<ListItem button component={NavLinkMui} to={to}>{text}</ListItem>

MUI v4 ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ (4.3.1์— ์žˆ์Œ) ์ดํ›„ ๊ฐ€์žฅ ๋งŽ์ด ๋“ํ‘œ ํ•œ ๋‹ต๋ณ€์€ "ํ•จ์ˆ˜ ๊ตฌ์„ฑ ์š”์†Œ์— ์ฐธ์กฐ๋ฅผ ์ œ๊ณต ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.์ด ์ฐธ์กฐ์— ์•ก์„ธ์Šคํ•˜๋ ค๋Š” ์‹œ๋„๊ฐ€ ์‹คํŒจํ•ฉ๋‹ˆ๋‹ค. React.forwardRef ()๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๊นŒ?"๋กœ ์ธํ•ด ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

NavLink ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ž˜ํผ๋กœ ๊ต์ฒดํ–ˆ์Šต๋‹ˆ๋‹ค.

import React, { Component } from "react"
import { NavLink } from "react-router-dom"

/**
 * React Router Nav Link wrapper to forward the ref to fix "Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?"
 *
 * From https://material-ui.com/guides/composition/#caveat-with-refs
 */
class NavLinkMui extends Component {
    render() {
        const { forwardedRef, ...props } = this.props
        return <NavLink {...props} ref={forwardedRef} />
    }
}

export default NavLinkMui

์šฉ๋ฒ•:

<ListItem
    button
    component={NavLinkMui}
    to='/'
>
    <ListItemIcon>
        <SlideshowIcon />
    </ListItemIcon>
</ListItem>

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค @HugoGresse ์ด๊ฒƒ์€ ์™„๋ฒฝํ•˜๊ฒŒ ์ž˜ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค :-)

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰