Material-ui: withStylesκ°€ μŠ€νƒ€μΌ κ°œμ²΄μ— μ†Œν’ˆμ„ 전달할 수 μžˆμŠ΅λ‹ˆκΉŒ?

에 λ§Œλ“  2017λ…„ 10μ›” 17일  Β·  54μ½”λ©˜νŠΈ  Β·  좜처: mui-org/material-ui

ν˜„μž¬ props와 theme 객체가 λͺ¨λ‘ ν•„μš”ν•œ ꡬ성 μš”μ†Œλ₯Ό 개발 μ€‘μž…λ‹ˆλ‹€.

μ²˜μŒμ—λŠ” ν…Œλ§ˆ κ°œμ²΄μ™€ 잘 μž‘λ™ν•©λ‹ˆλ‹€.

const styles = theme => ({
  title: {
    ...theme.typography.headline,
    textAlign: 'center',
    padding: '8px 16px',
    margin: 0,
    color: theme.palette.common.white,
    backgroundColor: theme.palette.primary[500],
  },

withStyles(styles, { withTheme: true })(Component);
....

ν•˜μ§€λ§Œ μŠ€νƒ€μΌ 개체의 μ†Œν’ˆμ—λ„ μ•‘μ„ΈμŠ€ν•΄μ•Ό ν•©λ‹ˆλ‹€.

예제λ₯Ό μ‹œλ„ν–ˆμ§€λ§Œ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

{
 ....
display: (props) => props.display
}

κ²°κ΅­ react-jss 와 withTheme λ₯Ό κ²°ν•©ν•˜μ—¬ κ·Έλ ‡κ²Œ ν•©λ‹ˆλ‹€.

import { withTheme } from 'material-ui/styles';
import injectSheet from 'react-jss';

function withStyles(styles, Component) {
  return withTheme()(injectSheet(styles)(Component));
}

export default withStyles;

....

const styles = {
  title: {
    display: (props) => props.display,
    textAlign: 'center',
    padding: '8px 16px',
    margin: 0,
    color: ({ theme }) => theme.palette.common.white,
    backgroundColor: ({ theme }) => theme.palette.primary[500],
  },

μž‘λ™ν•˜μ§€λ§Œ 정말 κ·Έλ¦¬μ›Œ

  title: {
    ...theme.typography.headline,
duplicate

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

@oliviertassinari 이것을 μš°μ„ μˆœμœ„λ‘œ κ³ λ €ν•  κ²ƒμ΄λΌλŠ” μ†Œμ‹μ„ λ“£κ²Œ λ˜μ–΄ 맀우 μ•ˆμ‹¬μ΄ λ©λ‹ˆλ‹€! μ΄λ ‡κ²Œ ν•˜λ©΄ ꡬ성 μš”μ†Œλ₯Ό 훨씬 μ‰½κ²Œ μ‚¬μš©μž 지정할 수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, ꡬ성 κ°€λŠ₯ν•œ 크기(예: λ„ˆλΉ„ 및 높이(ν”½μ…€))κ°€ μžˆλŠ” ν™•μΈλž€μ„ κ°–κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

<CustomCheckbox size={16} />

μš°λ¦¬κ°€ μ•‘μ„ΈμŠ€ ν•  μˆ˜μžˆλŠ” 경우 props μ—μ„œ styles 이 맀우 간단 ν•  κ²ƒμž…λ‹ˆλ‹€ :

const styles = {
  root: {
    width: props => props.size,
    height: props => props.size
  }
}

λ˜λŠ”

const styles = props => ({
  root: {
    width: props.size,
    height: props.size
  }
})

그리고:

const CustomCheckbox = ({size, classes}) => <Checkbox className={classes.root} />;

export default withStyles(styles)(CustomCheckbox);

ν˜„μž¬λ‘œμ„œλŠ” μ΄λŸ¬ν•œ μœ ν˜•μ˜ μ‚¬μš© 사둀에 μ ‘κ·Όν•˜λŠ” 방법에 λŒ€ν•œ ꢌμž₯ 사항이 μžˆμŠ΅λ‹ˆκΉŒ? λ˜λŠ” withStylesλ₯Ό μ‚¬μš©ν•  λ•Œ μ†Œν’ˆ μ•‘μ„ΈμŠ€ 지원을 μΆ”κ°€ν•  수 μžˆμ„ κ²ƒμœΌλ‘œ μ˜ˆμƒλ˜λŠ” μ‹œκΈ°κ°€ μžˆμŠ΅λ‹ˆκΉŒ?

λͺ¨λ“  54 λŒ“κΈ€

react-jss와 λ™μΌν•œ μ»¨ν…μŠ€νŠΈ ν‚€λ₯Ό μ‚¬μš©ν•˜μ—¬ Material-UIλ₯Ό λ§Œλ“€μ–΄ 문제λ₯Ό ν•΄κ²°ν•  수 μžˆμ„ κ²ƒμž…λ‹ˆλ‹€. https://github.com/cssinjs/theming/blob/master/src/channel.js#L1.
λ˜ν•œ #7633을 μ‚΄νŽ΄λ³΄μ‹­μ‹œμ˜€.

react-jss μƒν˜Έ μš΄μš©μ„± 예제둜 PR을 μ€€λΉ„ν–ˆμŠ΅λ‹ˆλ‹€. λ¬Έμ„œμ— μΆ”κ°€ν•˜κ² μŠ΅λ‹ˆλ‹€. cc @kof

@oliviertassinari 이 문제의 해결은 이제 μŠ€νƒ€μΌ μ •μ˜ λ‚΄μ—μ„œ μ†Œν’ˆμ— μ•‘μ„ΈμŠ€ν•  수 μžˆμ–΄μ•Ό ν•œλ‹€λŠ” 것을 μ˜λ―Έν•©λ‹ˆκΉŒ? 방법이 λͺ…ν™•ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€...

@pelotom μ•„λ‹ˆμš”, withStylesλŠ” 속성에 μ•‘μ„ΈμŠ€ν•  수 μ—†μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ μ–Όλ§ˆλ‚˜ λ§Žμ€ μ‚¬λžŒλ“€μ΄ 이 κΈ°λŠ₯을 μš”κ΅¬ν•˜λŠ”μ§€ κ°μ•ˆν•  λ•Œ. 버그 μˆ˜μ • ν›„ μš°μ„ μˆœμœ„λ₯Ό μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. injectionSheet HOCλ₯Ό μ‚¬μš©ν•  수 μžˆμ§€λ§Œ λ©”λͺ¨λ¦¬ λˆ„μˆ˜, ν•« λ¦¬λ‘œλ”© 쀑단, classes ꡬ성 μ—†μŒ, λ‚΄λΆ€ μ°Έμ‘° μ•‘μ„ΈμŠ€ μ—†μŒ, ν…Œλ§ˆ 쀑첩 처리 였λ₯˜ λ“± μ—¬λŸ¬ λ¬Έμ œκ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€. 적어도 그것은 λ‚΄κ°€ 과거에 μ§λ©΄ν–ˆλ˜ 문제 쀑 일뢀이며 λ‹€μ‹œ μž‘μ„±ν•˜λ„λ‘ 동기λ₯Ό λΆ€μ—¬ν–ˆμŠ΅λ‹ˆλ‹€. 그런 λ¬Έμ œλ“€μ΄ μ°¨κ·Όμ°¨κ·Ό 해결될 것이라고 μƒκ°ν•©λ‹ˆλ‹€.

@oliviertassinari 이것을 μš°μ„ μˆœμœ„λ‘œ κ³ λ €ν•  κ²ƒμ΄λΌλŠ” μ†Œμ‹μ„ λ“£κ²Œ λ˜μ–΄ 맀우 μ•ˆμ‹¬μ΄ λ©λ‹ˆλ‹€! μ΄λ ‡κ²Œ ν•˜λ©΄ ꡬ성 μš”μ†Œλ₯Ό 훨씬 μ‰½κ²Œ μ‚¬μš©μž 지정할 수 μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, ꡬ성 κ°€λŠ₯ν•œ 크기(예: λ„ˆλΉ„ 및 높이(ν”½μ…€))κ°€ μžˆλŠ” ν™•μΈλž€μ„ κ°–κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

<CustomCheckbox size={16} />

μš°λ¦¬κ°€ μ•‘μ„ΈμŠ€ ν•  μˆ˜μžˆλŠ” 경우 props μ—μ„œ styles 이 맀우 간단 ν•  κ²ƒμž…λ‹ˆλ‹€ :

const styles = {
  root: {
    width: props => props.size,
    height: props => props.size
  }
}

λ˜λŠ”

const styles = props => ({
  root: {
    width: props.size,
    height: props.size
  }
})

그리고:

const CustomCheckbox = ({size, classes}) => <Checkbox className={classes.root} />;

export default withStyles(styles)(CustomCheckbox);

ν˜„μž¬λ‘œμ„œλŠ” μ΄λŸ¬ν•œ μœ ν˜•μ˜ μ‚¬μš© 사둀에 μ ‘κ·Όν•˜λŠ” 방법에 λŒ€ν•œ ꢌμž₯ 사항이 μžˆμŠ΅λ‹ˆκΉŒ? λ˜λŠ” withStylesλ₯Ό μ‚¬μš©ν•  λ•Œ μ†Œν’ˆ μ•‘μ„ΈμŠ€ 지원을 μΆ”κ°€ν•  수 μžˆμ„ κ²ƒμœΌλ‘œ μ˜ˆμƒλ˜λŠ” μ‹œκΈ°κ°€ μžˆμŠ΅λ‹ˆκΉŒ?

@nmchaves μ‚¬μš© μ‚¬λ‘€λŠ” 인라인 μŠ€νƒ€μΌ μ ‘κ·Ό 방식에 μ™„λ²½ν•˜κ²Œ λΆ€ν•©ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ„€λͺ…μ„œμ—μ„œ 이에 λŒ€ν•΄ 쑰금 찾을 수 μžˆμŠ΅λ‹ˆλ‹€. μžμ£Όν•˜λŠ” 질문
https://github.com/callemall/material-ui/blob/75a30061e76eae93c711ec202a2c7e4238a4f19a/docs/src/pages/style/SvgIcons.js#L38 -L44

@oliviertassinari 감사 withStyles μ‚¬μš©ν•˜μ—¬ 이 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 있기λ₯Ό λ°”λžμ§€λ§Œ 인라인 μŠ€νƒ€μΌμ΄ 잘 μž‘λ™ν•  κ²ƒμž…λ‹ˆλ‹€. 그리고 λ¬Έμ„œμ—μ„œ μ—¬κΈ° +λ₯Ό ꢌμž₯ν•œλ‹€λŠ” 사싀은 이 결정에 λŒ€ν•΄ 맀우 확신을 κ°–κ²Œ ν•©λ‹ˆλ‹€. λ‹€μ‹œ ν•œλ²ˆ κ°μ‚¬ν•©λ‹ˆλ‹€!

backgroundImage의 μŠ€νƒ€μΌμ— μ†Œν’ˆ(이미지 src)을 전달할 수 있으면 쒋을 κ²ƒμž…λ‹ˆλ‹€.

withStyle 포μž₯ν•˜κ² μŠ΅λ‹ˆλ‹€

const withStylesProps = styles =>
  Component =>
    props => {
      console.log(props);
      const Comp = withStyles(styles(props))(Component);
      // return <div>lol</div>;
      return <Comp {...props} />;
    };

const styles = props => ({
  foo: {
    height: `${props.y || 50}px`,
  }
});

export default withStylesProps(styles)(
  props => (
    <div className={props.classes.foo} style={{ ...props.style, background: 'yellow' }}>
      <h1>Hello!</h1>
    </div>
  )
);

데λͺ¨: https://codesandbox.io/s/k2y01rj3w7

(λ†€λžμŠ΅λ‹ˆλ‹€ ^ ThemeProvider 및 JssProvider μ„€μ • 없이 https://codesandbox.io/s/q6v7krx6, μ•„ μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€)

@caub μž‘λ™ μ€‘μ΄μ§€λ§Œ 이 νŒ¨ν„΄μ— μ£Όμ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. μ‚½μž…λœ CSSλŠ” ꡬ성 μš”μ†Œμ˜ μΈμŠ€ν„΄μŠ€ μˆ˜μ™€ ν•¨κ»˜ μ»€μ§‘λ‹ˆλ‹€. #7633의 λ³΅μ œν’ˆμž…λ‹ˆλ‹€. λ‚˜λŠ” 주제λ₯Ό νŒŒν—€μΉ˜μ§€ μ•Šμ•˜λ‹€. κ·ΈλŸ¬λ‚˜ @kof 버전은 일뢀 μ„±λŠ₯ μ΅œμ ν™”λ₯Ό μ‚¬μš©ν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

@caub 곡유

@oliviertassinari https://github.com/cssinjs/react-jss/blob/master/readme.md#dynamic -values ​​in react-jssκ°€ μžˆμŠ΅λ‹ˆλ‹€. μ™œ material-uiμ—μ„œ μ‚¬μš©ν•  수 μ—†λŠ”μ§€ κΆκΈˆν•©λ‹ˆλ‹€. λ˜ν•œ 인라인 style prop이 동적 값에 μ ν•©ν•˜μ§€λ§Œ λͺ¨λ“  μŠ€νƒ€μΌ μ •μ˜λ₯Ό 같은 μœ„μΉ˜μ— λ‘λŠ” 것이 더 λ‚«λ‹€κ³  λ§μ”€ν•˜μ‹  μš”μ μ„ μ΄ν•΄ν•©λ‹ˆλ‹€. 보닀 효율적인 동적 μŠ€νƒ€μΌμ„ μœ„ν•΄ className 및 style λ₯Ό μ²˜λ¦¬ν•˜λŠ” https://github.com/airbnb/react-with-styles 도

λ‚˜λŠ” 같은 λ¬Έμ œμ— 직면 ν•΄μžˆλ‹€. λˆ„κ΅°κ°€ λ‚˜λ₯Ό λ„μšΈ 수 μžˆμŠ΅λ‹ˆκΉŒ?
`'react'μ—μ„œ React κ°€μ Έμ˜€κΈ°;
'prop-types'μ—μ„œ PropType을 κ°€μ Έμ˜΅λ‹ˆλ‹€.
'material-ui/styles'μ—μ„œ { withStyles } κ°€μ Έμ˜€κΈ°;
'material-ui/Drawer'μ—μ„œ μ„œλž κ°€μ Έμ˜€κΈ°;
'material-ui/AppBar'μ—μ„œ AppBar κ°€μ Έμ˜€κΈ°;
'material-ui/Toolbar'μ—μ„œ 도ꡬ λͺ¨μŒ κ°€μ Έμ˜€κΈ°;
'material-ui/List'μ—μ„œ λͺ©λ‘ κ°€μ Έμ˜€κΈ°;
'material-ui/Typography'μ—μ„œ νƒ€μ΄ν¬κ·Έλž˜ν”Ό κ°€μ Έμ˜€κΈ°;
'material-ui/IconButton'μ—μ„œ IconButton κ°€μ Έμ˜€κΈ°;
import Hidden from 'material-ui/Hidden';
'material-ui/Divider'μ—μ„œ 디바이더 κ°€μ Έμ˜€κΈ°;
'material-ui-icons/Menu'μ—μ„œ MenuIcon κ°€μ Έμ˜€κΈ°;
'./tileData'μ—μ„œ { mailFolderListItems, otherMailFolderListItems } κ°€μ Έμ˜€κΈ°;

const μ„œλž λ„ˆλΉ„ = 240;

const μŠ€νƒ€μΌ = ν…Œλ§ˆ => ({
루트: {
λ„ˆλΉ„: '100%',
ν‚€: 430,
marginTop: theme.spacing.unit * 3,
z인덱슀: 1,
μ˜€λ²„ν”Œλ‘œ: 'μˆ¨κΉ€',
},
μ•±ν”„λ ˆμž„: {
μœ„μΉ˜: 'μƒλŒ€μ ',
λ””μŠ€ν”Œλ ˆμ΄: 'ν”Œλ ‰μŠ€',
λ„ˆλΉ„: '100%',
높이: '100%',
},
μ•±λ°”: {
μœ„μΉ˜: 'μ ˆλŒ€',
marginLeft: μ„œλž λ„ˆλΉ„,
[theme.breakpoints.up('md')]: {
λ„ˆλΉ„: calc(100% - ${drawerWidth}px) ,
},
},
navIconHide: {
[theme.breakpoints.up('md')]: {
ν‘œμ‹œ: 'μ—†μŒ',
},
},
μ„œλž 헀더: theme.mixins.toolbar,
μ„œλžμš©μ§€: {
λ„ˆλΉ„: 250,
[theme.breakpoints.up('md')]: {
λ„ˆλΉ„: μ„œλž λ„ˆλΉ„,
μœ„μΉ˜: 'μƒλŒ€μ ',
높이: '100%',
},
},
μ½˜ν…μΈ : {
backgroundColor: theme.palette.background.default,
λ„ˆλΉ„: '100%',
νŒ¨λ”©: theme.spacing.unit * 3,
높이: '계산(100% - 56px)',
여백상단: 56,
[theme.breakpoints.up('sm')]: {
높이: '계산(100% - 64px)',
여백상단: 64,
},
},
});

내보내기 클래슀 ResponsiveDrawerλŠ” React.Component {λ₯Ό ν™•μž₯ν•©λ‹ˆλ‹€.
μƒνƒœ = {
mobileOpen: 거짓,
};

handleDrawerToggle = () => {
this.setState({ mobileOpen: !this.state.mobileOpen });
};

λ Œλ”λ§() {
const { 클래슀, ν…Œλ§ˆ } = this.props;

const drawer = (
  <div>
    <div className={classes.drawerHeader} />
    <Divider />
    <List>{mailFolderListItems}</List>
    <Divider />
    <List>{otherMailFolderListItems}</List>
  </div>
);

return (
  <div className={classes.root}>
    <div className={classes.appFrame}>
      <AppBar className={classes.appBar}>
        <Toolbar>
          <IconButton
            color="inherit"
            aria-label="open drawer"
            onClick={this.handleDrawerToggle}
            className={classes.navIconHide}
          >
            <MenuIcon />
          </IconButton>
          <Typography variant="title" color="inherit" noWrap>
            Responsive drawer
          </Typography>
        </Toolbar>
      </AppBar>
      <Hidden mdUp>
        <Drawer
          variant="temporary"
          anchor={theme.direction === 'rtl' ? 'right' : 'left'}
          open={this.state.mobileOpen}
          classes={{
            paper: classes.drawerPaper,
          }}
          onClose={this.handleDrawerToggle}
          ModalProps={{
            keepMounted: true, // Better open performance on mobile.
          }}
        >
          {drawer}
        </Drawer>
      </Hidden>
      <Hidden smDown implementation="css">
        <Drawer
          variant="permanent"
          open
          classes={{
            paper: classes.drawerPaper,
          }}
        >
          {drawer}
        </Drawer>
      </Hidden>
      <main className={classes.content}>
        <Typography noWrap>{'You think water moves fast? You should see ice.'}</Typography>
      </main>
    </div>
  </div>
);

}
}

ResponsiveDrawer.propTypes = {
클래슀: PropTypes.object.isRequired,
ν…Œλ§ˆ: PropTypes.object.isRequired,
};

κΈ°λ³Έ 내보내기 withStyles(styles)(ResponsiveDrawer);
`

screen shot 2018-02-13 at 3 27 41 am

μ‚½μž…λœ CSSλŠ” ꡬ성 μš”μ†Œμ˜ μΈμŠ€ν„΄μŠ€ μˆ˜μ™€ ν•¨κ»˜ μ»€μ§‘λ‹ˆλ‹€.

@oliviertassinari μ£Όμž… CSSλŠ” +- html이 인라인 μŠ€νƒ€μΌλ‘œ μ„±μž₯ν•˜λŠ” 것과 같은 λ°©μ‹μœΌλ‘œ μ„±μž₯ν•  κ²ƒμž…λ‹ˆλ‹€. 정적 μŠ€νƒ€μΌμ€ λ³„λ„μ˜ μ‹œνŠΈμ— λ Œλ”λ§λ˜κ³  λͺ¨λ“  ꡬ성 μš”μ†Œ μΈμŠ€ν„΄μŠ€μ—μ„œ μž¬μ‚¬μš©λ©λ‹ˆλ‹€.

λΉ„ μ €μž₯ ꡬ성 μš”μ†Œκ°€ λ‹€μ‹œ λ Œλ”λ§ν•˜μ§€λ§Œ λ‚˜λŠ”μ΄ μ’‹μ•„ν–ˆλ‹€ withStyle μ—μ„œ render 에 render , μš°λ¦¬λŠ” 전체 μˆœμˆ˜ν•œ ꡬ성 μš”μ†Œλ₯Ό μ‚¬μš©ν•˜μ—¬ ν”Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

import React from 'react';
import {
  withStyles,
  Grid,
  CircularProgress
} from 'material-ui';

const PreloadComponent = props => {
  const { classes,size } = props;
  return (
    <Grid className={classes.container} container justify={'center'} alignItems={'center'}>
      <CircularProgress size={size}/>
    </Grid>
  )
};

const StyleWithThemeProps = (props) => {
  return withStyles(theme => ({
    container: {
      paddingTop: props.size*2 || 50,
      paddingBottom: props.size*2 || 50,
    }
  }),{withTheme: true})(PreloadComponent)
};

const Preload = props => {
  const { size } = props;
  const WithStylesPreloadComponent = StyleWithThemeProps(props);
  return (
    <WithStylesPreloadComponent {...props}/>
  )
};

Preload.defaultProps = {
  size: 20
};

export default Preload;

μ—…λ°μ΄νŠΈλ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ μ™„μ „ν•œ 순수 ꡬ성 μš”μ†Œλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const PreloadComponent = props => {
  const { classes,size } = props;
  return (
    <Grid className={classes.container} container justify={'center'} alignItems={'center'}>
      <CircularProgress size={size}/>
    </Grid>
  )
};

const StyleWithThemeProps = (props) => {
  return withStyles(theme => ({
    container: {
      paddingTop: props.size*2 || 50,
      paddingBottom: props.size*2 || 50,
    }
  }),{withTheme: true})(PreloadComponent)
};

class PreloadFull extends React.PureComponent {

  constructor(props,context) {
    super(props);
  }

  componentWillMount() {
    this.StyledPreloadFull = StyleWithThemeProps(this.props);
  }

  componentWillUpdate(nextProps) {
    this.StyledPreloadFull = StyleWithThemeProps(nextProps);
  }

  render() {
    const { StyledPreloadFull,props } = this;
    return (
      <StyledPreloadFull {...props}/>
    );
  }
}

PreloadFull.defaultProps = {
  size: 20
};

export default PreloadFull;

@up209d μž‘λ™ν•˜μ§€λ§Œ κ½€ κ³ ν†΅μŠ€λŸ½μŠ΅λ‹ˆλ‹€. withStyles λ₯Ό μˆ˜μ •ν•˜μ—¬ 값에 μ†Œν’ˆμ„ 전달할 수 μžˆλŠ” https://github.com/cssinjs/react-jss λ₯Ό 보닀 μ§μ ‘μ μœΌλ‘œ μ‚¬μš©ν•˜λ €κ³  ν•©λ‹ˆλ‹€.

@SrikanthChebrolu μ£Όμ œκ°€ μ•„λ‹ˆλ―€ 둜 λ©”μ‹œμ§€λ₯Ό λ‹€λ₯Έ 문제둜 이동할 수 μžˆμŠ΅λ‹ˆκΉŒ?

이 μƒνƒœκ°€ 어떀지 κΆκΈˆν•˜μ„Έμš”? 이 문제, JSS λ¬Έμ„œ, material-ui λ¬Έμ„œλ₯Ό μ½μ—ˆμ§€λ§Œ 아직 인라인 μŠ€νƒ€μΌμ„ μ‚¬μš©ν•  ν•„μš”κ°€ μ—†λŠ” Mui+Jss+TypeScript에 λŒ€ν•œ μ†”λ£¨μ…˜μ„ 찾지 λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. λͺ‡ 가지 인라인 μŠ€νƒ€μΌμ„ λ„£λŠ” 것이 λ•Œλ‘œλŠ” λΆˆκ°€ν”Όν•˜μ§€λ§Œ 제 κ²½μš°μ—λŠ” ν…Œλ§ˆμ™€ μ†Œν’ˆμ— λͺ¨λ‘ μ˜μ‘΄ν•˜λŠ” λ‹€μ–‘ν•œ μƒνƒœλ₯Ό 가진 μ—¬λŸ¬ μŠ€νƒ€μΌμ΄ μžˆμŠ΅λ‹ˆλ‹€.

@chazsolo Hey Chaz, μ‹€μ œλ‘œ withStyles from mui λŒ€μ‹  injectSheet from react-jss λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 그런 μ‹μœΌλ‘œ props 및 theme λ‘˜ λ‹€ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

import injectSheet from 'react-jss';

const styles = theme => ({
  container: {
     color: props => theme.palette[props.color || 'primary'].main
  }
});

...

export default injectSheet(styles)(AnyComponent);
import { JssProvider, jss, createGenerateClassName } from 'react-jss/lib';
import { MuiThemeProvider } from 'material-ui';

const generateClassName = createGenerateClassName();

...

<JssProvider jss={jss} generateClassName={generateClassName}>
  <MuiThemeProvider theme={props.theme} sheetsManager={new Map()}>
    <App/>
  </MuiThemeProvider>
</JssProvider>

@chazsolo 이 문제λ₯Ό νŒ”λ‘œμš°ν•˜κ³  μ‹Άλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€ https://github.com/cssinjs/jss/issues/682

@kof 와 @up209d μ—κ²Œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€. κ΅¬λ…ν•˜κ³  up209d의 예λ₯Ό ν•œ 번 λ³΄μ—¬μ£Όμ„Έμš”.

@up209d
λΆˆν–‰νžˆλ„ λ‚˜λŠ” 그것이 λ‚˜μ—κ²Œ νš¨κ³Όμ μ΄μ§€ μ•Šμ„ 것이라고 μƒκ°ν•©λ‹ˆλ‹€. κ·€ν•˜κ°€ μ œμ•ˆν•œ 것을 κ΅¬ν˜„ν–ˆμœΌλ©° styles 개체 λ‚΄λΆ€μ˜ ν•¨μˆ˜ 호좜 λ‚΄μ—μ„œ μ†Œν’ˆμ„ λ³Ό 수 μžˆμ§€λ§Œ 계속 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. λ‚΄κ°€ μœ ν˜•μ„ λ†“μΉ˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? props Interfacesμ—μ„œ WithStyles λ₯Ό ν™•μž₯ν•˜κ³  μžˆμœΌλ―€λ‘œ props의 classes κ°œμ²΄μ— μ•‘μ„ΈμŠ€ν•  수 μžˆμŠ΅λ‹ˆλ‹€(이제 https://github.com/mui-μ—μ„œ μ°Έμ‘°ν•œ λ¬Έμ œμΈμ§€ κΆκΈˆν•©λ‹ˆλ‹€. org/material-ui/issues/8726#issuecomment-337482040)

TS2344: Type '(theme: ITheme) => { arc: { stroke: string; strokeWidth: (props: any) => string | number; }; arcM...' does not satisfy the constraint 'string | Record<string, CSSProperties> | StyleRulesCallback<string>'.
  Type '(theme: ITheme) => { arc: { stroke: string; strokeWidth: (props: any) => string | number; }; arcM...' is not assignable to type 'StyleRulesCallback<string>'.
    Type '{ arc: { stroke: string; strokeWidth: (props: any) => string | number; }; arcMovement: { strokeDa...' is not assignable to type 'Record<string, CSSProperties>'.
      Property 'arc' is incompatible with index signature.
        Type '{ stroke: string; strokeWidth: (props: any) => string | number; }' is not assignable to type 'CSSProperties'.
          Types of property 'strokeWidth' are incompatible.
            Type '(props: any) => string | number' is not assignable to type 'string | number | undefined'.
              Type '(props: any) => string | number' is not assignable to type 'number'.

λ‚΄ ν…Œλ§ˆλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

import { ITheme } from '...';

export default (theme: ITheme) => ({
  arc: {
    // ...
    strokeWidth: (props: any): number | string => {
      // this logs the correct data I'm expecting
      console.log(props.data[0].properties.name)
      return 1.5
    }
  },
  arcMovement: {
    // ...
  },
})

ν₯미둜운 점은 λ‚΄ ꡬ성 μš”μ†Œ λ‚΄μ—μ„œ classes 개체λ₯Ό μ‚¬μš©ν•  λ•Œ arc 및 arcMovement κ°€ μœ νš¨ν•œ μ†μ„±μ΄λΌλŠ” κ²ƒμž…λ‹ˆλ‹€.

// from Chrome console
{
  arc: "Arcs-arc-0-2-1 Arcs-arc-0-2-3",
  arcMovement: "Arcs-arcMovement-0-2-2"
}

μ—…λ°μ΄νŠΈ

이 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμ—ˆμ§€λ§Œ μœ„μ˜ μ£Όμ„μ—μ„œ μ–ΈκΈ‰ν–ˆλ“―μ΄ WithStyles , withStyles λŒ€ν•œ λͺ¨λ“  μ°Έμ‘°λ₯Ό μ œκ±°ν•΄μ•Ό ν–ˆμœΌλ©° classes ꡬ성 및 ν…Œλ§ˆλ₯Ό μžƒμ–΄λ²„λ ΈμŠ΅λ‹ˆλ‹€. 쀑첩. λ‚˜λŠ” 이제 쉬고 μŠ€λ ˆλ“œλ₯Ό μ£Όμ‹œν•  κ²ƒμž…λ‹ˆλ‹€. λͺ¨λ“  도움에 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€!

@chazsolo Hey Chaz, ν™•μ‹€ν•˜μ§€ μ•Šμ§€λ§Œ style 개체의 props 내뢀에 μžˆλŠ” classes 에 μ•‘μ„ΈμŠ€ν•˜λ €λŠ” κ²ƒμž…λ‹ˆλ‹€. κ·Έλ ‡λ‹€λ©΄, λ‚˜λŠ” 그것이 λΆˆκ°€λŠ₯ν•˜λ‹€κ³  생각뢀터 classes 만 μ‚¬μš©ν•  수 ν›„ jss 처리 style 객체 μ–΄λ–»κ²Œ μ•‘μ„ΈμŠ€ ν•  수 classes κ²°μ •μ˜ κ³Όμ • 전에 classes 도 μ‹€ν–‰λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆκΉŒ?

@caubκ°€ 이미 μ†”λ£¨μ…˜μ„ μ œκ³΅ν–ˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. μ•½κ°„μ˜ λΉ„ν‹€κΈ°λ‘œ μ†”λ£¨μ…˜μ„ λ‹€μ‹œ κ²Œμ‹œν•˜μ‹­μ‹œμ˜€. μΆ”κ°€ λΌμ΄λΈŒλŸ¬λ¦¬κ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

μžμ‹ λ§Œμ˜ 래퍼 withStylesProps λΉŒλ“œν•©λ‹ˆλ‹€.

import { withStyles } from 'material-ui/styles';

const styles = ( theme, props ) => ({
    exampleStyle: {
           color: 'red'  // <-- or try theme.palette.primary[600]
    }
})

const withStylesProps = ( styles ) =>
  Component =>
    props => {
      const Comp = withStyles(theme => styles(theme, props))(Component);
      return <Comp {...props} />;
    };

const YourComponent = ({  classes }) => 
      <Typography type="display4" className={classes.exampleStyle}>{type}</Typography>

export default withStylesProps(styles)(YourComponent);

λͺ¨λ“  ꡬ성 μš”μ†Œμ— λŒ€ν•΄ withStylesProps λ§Œλ“€κΈ°κ°€ λ§ˆμŒμ— 듀지 μ•ŠμœΌλ©΄ λ³„λ„μ˜ νŒŒμΌμ— μΆ”κ°€ν•˜κ³  μ›ν•˜λŠ” μœ„μΉ˜μ— κ°€μ Έμ˜΅λ‹ˆλ‹€.

@iamthuypham 팁 κ°μ‚¬ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ withStylesProps 둜 ꡬ성 μš”μ†Œλ₯Ό λž˜ν•‘ν•˜λ©΄ λž˜ν•‘λœ ꡬ성 μš”μ†Œ λ‚΄λΆ€μ˜ μ–΄λ”˜κ°€μ—μ„œ μ‚¬μš©ν•˜κ³  μžˆλŠ” μ „ν™˜ ꡬ성 μš”μ†Œ <Collapse 의 μ• λ‹ˆλ©”μ΄μ…˜μ΄ μž‘λ™μ„ 멈μΆ₯λ‹ˆλ‹€.

@jdolinski1 μ½”λ“œ 예제λ₯Ό 볡사/λΆ™μ—¬λ„£κΈ° ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

@iamthuypham κ·€ν•˜μ˜ μ†”λ£¨μ…˜μ—λŠ” ꡬ성 μš”μ†Œκ°€ 생성될 λ•Œλ§ˆλ‹€ μƒˆλ‘œμš΄ <style> νƒœκ·Έλ₯Ό μƒμ„±ν•˜λŠ” 단점이 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ defaultPropsλ₯Ό μ‚¬μš©ν•  λ•Œ μ£Όμ˜ν•˜κ³  κΈ°λ³Έ ꡬ성 μš”μ†Œκ°€ μ•„λ‹Œ HOC ꡬ성 μš”μ†Œμ— μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

image

이 λͺ¨λ“  것이 react-jss μ—μ„œ μ§€μ›λ˜λ©°, 기본적으둜 material-ui μ§€μ›λ˜μ§€ μ•ŠμŠ΅λ‹ˆκΉŒ?

λ˜ν•œ @jdolinski1 의 λ¬Έμ œλŠ” μ½”λ“œκ°€ λž˜ν•‘λœ ꡬ성 μš”μ†Œμ— μžˆμ„ 수 μžˆλŠ” children μ „νŒŒν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

@iamthuypham μ˜ˆμ „μ— 그랬던 κ²ƒμ²˜λŸΌ κ·Έλ ‡κ²Œ ν•˜λŠ” 것은 ꢌμž₯ν•˜μ§€ μ•ŠλŠ” 것 κ°™κ³ , 앱이 빨리 μ„±μž₯ν•˜λŠ” ν•œ μ„±λŠ₯이 μ €ν•˜λ  수 μžˆμŠ΅λ‹ˆλ‹€. μƒˆλ‘œμš΄ jss style 객체둜 component 의 μƒˆ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” 것은 style 객체가 μ™„μ „νžˆ λ‹€μ‹œ λ Œλ”λ§λ˜μ–΄μ•Ό ν•˜κΈ° λ•Œλ¬Έμ— μ½”λ”© 원칙 μΈ‘λ©΄μ—μ„œ 쒋지 μ•ŠμŠ΅λ‹ˆλ‹€. props λ³€κ²½λ‹Ή μ‹œκ°„. injectSheet μ—μ„œ react-jss 것이 더 λ‚˜μ€ μ„ νƒμž…λ‹ˆλ‹€. injectSheet λ₯Ό μ‚΄νŽ΄λ³΄λ©΄ style 개체λ₯Ό 2개의 쑰각( static & dynamic )으둜 λ‚˜λˆ„λ―€λ‘œ dynamic props λ³€κ²½λ˜λ©΄ dynamic κ°€ λ‹€μ‹œ λ Œλ”λ§λ©λ‹ˆλ‹€.

InjectSheet와 ν•¨κ»˜ jss-nested와 같은 ν”ŒλŸ¬κ·ΈμΈμ„ μ‚¬μš©ν•˜λŠ” 방법은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

μ£Όμž… μ‹œνŠΈλ₯Ό μ‚¬μš©ν•˜λ©΄ '&:hover'문이 μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
withStylesλ₯Ό μ‚¬μš©ν•˜λ©΄ μ†Œν’ˆμ— μ•‘μ„ΈμŠ€ν•  수 μ—†μŠ΅λ‹ˆλ‹€...

@koutsenko λ‹€μŒμ€ μ˜ˆμž…λ‹ˆλ‹€.

import React from "react";
import { makeStyles } from "@material-ui/styles";
import Button from "@material-ui/core/Button";

const useStyles = makeStyles({
  root: {
    background: props => props.color,
    "&:hover": {
      background: props => props.hover
    },
    border: 0,
    borderRadius: 3,
    color: "white",
    height: 48,
    padding: "0 30px"
  }
});

export default function Hook() {
  const classes = useStyles({
    color: "red",
    hover: "blue"
  });
  return <Button className={classes.root}>Hook</Button>;
}

https://codesandbox.io/s/pw32vw2j3m

도움이 되기λ₯Ό λ°”λžλ‹ˆλ‹€.


와, ~1λ…„ λ™μ•ˆ μš°λ¦¬κ°€ μ΄λ£©ν•œ λ°œμ „μ΄ λ†€λžμŠ΅λ‹ˆλ‹€πŸ˜.

이제 μ–΄λ–»κ²Œ νƒ€μ΄ν•‘ν•©λ‹ˆκΉŒ?

@stunaz 쒋은 μ§ˆλ¬Έμž…λ‹ˆλ‹€. λͺ¨λ₯΄κ² μ–΄μš”. λ‚˜λŠ” 그것을 μ‘°μ‚¬ν•˜μ§€ μ•Šμ•˜λ‹€. @eps1lon 은 λͺ¨λ“ˆμ˜ TypeScript μ •μ˜λ₯Ό μ™„λ£Œν–ˆμŠ΅λ‹ˆλ‹€. μ‹œμž‘μ μœΌλ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
https://github.com/mui-org/material-ui/blob/f4281a77d15b0d6eec9d33cdc358cfb89844996d/packages/material-ui-styles/src/index.d.ts#L72

@koutsenko λ‹€μŒμ€ μ˜ˆμž…λ‹ˆλ‹€.

@oliviertassinari κ°μ‚¬ν•©λ‹ˆλ‹€. " react@next "λ₯Ό μ‚¬μš©ν•˜λ©΄ 이제 μž‘λ™ν•©λ‹ˆλ‹€.

@koutsenko jss-nested μž‘λ™ν•˜μ§€ μ•ŠμœΌλ©΄ μ½”λ”©μ˜ μ–΄λ”˜κ°€μ— ꡬ성 λ¬Έμ œκ°€ μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. jss-nested κ°€ jss-default-preset 에 ν¬ν•¨λ˜μ–΄ μžˆμœΌλ―€λ‘œ oob

https://stackblitz.com/edit/react-py6w2v

@oliviertassinari

μ†Œν’ˆμ„ μ‚¬μš©ν•˜μ—¬ 주어진 μ„ νƒκΈ°μ˜ 전체 μŠ€νƒ€μΌ 개체λ₯Ό μ„€μ •ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆκΉŒ? μ‘°κ±΄λΆ€λ‘œ 속성을 μ μš©ν•  수 μžˆλŠ” 곳은 μ–΄λ””μΈκ°€μš”?

예λ₯Ό λ“€μ–΄ 이와 같이

withStyles({
    root: {
        '& > path': (props) => {
            if(props.color)
                return {
                    fill: props.color
                };
           return {};
        }
    }
})

κ·Έλž˜μ„œ μ†Œν’ˆμ΄ μ‘΄μž¬ν•˜μ§€ μ•ŠμœΌλ©΄ λ‚΄κ°€ μ„€μ •ν•΄μ•Ό ν•˜λŠ” λ‹€λ₯Έ 값이 μ•„λ‹Œ 이전 μ±„μš°κΈ° 값을 μ‚¬μš©ν•©λ‹ˆκΉŒ? 예λ₯Ό λ“€μ–΄, 일반적으둜 μ±„μš°κΈ°μ— μ μš©λ˜λŠ” λ‹€λ₯Έ κ·œμΉ™μ΄ μžˆμ§€λ§Œ color μ†Œν’ˆμ΄ μ„€μ •λœ κ²½μš°μ—λ§Œ 이 μƒˆ μ±„μš°κΈ° 속성을 μ„€μ •ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

감사 ν•΄μš”!

@Guardiannw μ–΄λ–€ 이유둜 κ·€ν•˜μ˜ λ³€ν˜•μ΄ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. @kof κ°€ 이유 πŸ’‘μ— λŒ€ν•œ

// πŸ†
const useStyles = makeStyles({
  root: {
    "& > span": {
      backgroundColor: props => props.color || null,
    }
  }
});

// or 

const useStyles = makeStyles({
  root: props => ({
    "& > span": {
      backgroundColor: props.color || null
    }
  })
});

@oliviertassinari withStyles κΈ°λŠ₯을 μ‚¬μš©ν•  수 μžˆλŠ” 두 번째 μ˜΅μ…˜μ„ μ–»λŠ” 데 어렀움을 κ²ͺκ³  μžˆμŠ΅λ‹ˆλ‹€. makeStyles 및 ν›„ν¬μ—μ„œλ§Œ μž‘λ™ν•©λ‹ˆκΉŒ?

@Guardiannw @material-ui/styles 의 API 쀑 ν•˜λ‚˜μ™€ μž‘λ™ν•©λ‹ˆλ‹€.

@oliviertassinari λŠ” μœ νš¨ν•œ ꡬ문처럼 λ³΄μž…λ‹ˆλ‹€. fn 값이 v10에 μΆ”κ°€λ˜μ—ˆμœΌλ―€λ‘œ v9κ°€ μ‚¬μš©λ˜μ—ˆκ±°λ‚˜ μ½”λ“œ 및 μƒμž μž¬μƒμ΄ ν•„μš”ν•©λ‹ˆλ‹€.

μ’‹μ•„, 그게 λ‚΄κ°€ 그것을 μ‹œλ„ν•œ κ²ƒμž…λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•΄μ•Ό ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

@oliviertassinari @materia-ui/styles μ‚¬μš©μ— λŒ€ν•΄ 질문이 μžˆμŠ΅λ‹ˆλ‹€. ν”„λ‘œλ•μ…˜ ν™˜κ²½μ—μ„œ μ‚¬μš©ν•  수 있고 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? μ„€λͺ…μ„œμ— μ•ˆμ •μ μΈ λ²„μ „μ—μ„œλŠ” μž‘λ™ν•˜μ§€ μ•ŠλŠ”λ‹€κ³  λ‚˜μ™€ μžˆμŠ΅λ‹ˆλ‹€. μ €λŠ” " 3.9.1", κ·€ν•˜κ°€ μ œμ‹œν•˜λŠ” https://github.com/mui-org/material-ui/issues/8726#issuecomment -452047345 μ˜ˆμ—λŠ” λ‚΄κ°€ ν•„μš”λ‘œ ν•˜λŠ” κ°•λ ₯ν•˜κ³  μœ μš©ν•œ κΈ°λŠ₯이 μžˆμŠ΅λ‹ˆλ‹€. μ΄λŸ¬ν•œ λ¬Έμ œμ—, λ‚˜λŠ” λ‹€λ₯Έ κ΄€μ μ—μ„œ λ§Žμ€ μ˜κ²¬μ„λ³΄κ³ λ„ λ‚˜λŠ” ν•΄κ²°μ±… μ’‹μ•„ https://github.com/mui-org/material-ui/issues/8726#issuecomment -363546636 @caub의, κ·ΈλŸ¬λ‚˜μ— λŒ€ν•œ μ˜κ²¬μ„ μžμ‹ μ˜ μ†”λ£¨μ…˜μ΄ μ’‹μŠ΅λ‹ˆλ‹€.

@contrerasjf0 @material-ui/styles λŠ” μ•ŒνŒŒ 릴리슀둜만 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” μ•ŒνŒŒ 버전을 λ°˜μ‘ μƒνƒœκ³„μ˜ λŒ€λΆ€λΆ„μ˜ νŒ¨ν‚€μ§€μ²˜λŸΌ μ·¨κΈ‰ν•©λ‹ˆλ‹€. ν”„λ‘œλ•μ…˜ ν™˜κ²½μ—μ„œλŠ” μ•ŒνŒŒ νŒ¨ν‚€μ§€λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. κ·Έλ ‡κ²Œ ν•˜λ©΄ λͺ¨λ“  릴리슀 간에 버그와 μ£Όμš” λ³€κ²½ 사항을 μ˜ˆμƒν•΄μ•Ό ν•©λ‹ˆλ‹€. 즉, 변동 μ•ŒνŒŒ 버전 μΆ”κ°€λ₯Ό μ²˜λ¦¬ν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

λ‚΄κ°€ λ°”λΌλŠ” 것은 μ‚¬λžŒλ“€μ΄ μ·¨λ―Έ ν”„λ‘œμ νŠΈμ—μ„œ μ΄λŸ¬ν•œ 버전을 μ‚¬μš©ν•˜κ±°λ‚˜ ν”„λ‘œλ•μ…˜μ— λ°°ν¬λ˜μ§€ μ•Šμ•˜μ§€λ§Œ ν”„λ‘œλ•μ…˜ λΆ„κΈ°μ²˜λŸΌ μ—¬μ „νžˆ ν…ŒμŠ€νŠΈλ˜λŠ” λ³„λ„μ˜ λΆ„κΈ°μ—μ„œ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. μ•ŒνŒŒ 버전을 μ‚¬μš©ν•˜κ³  ν”Όλ“œλ°±μ„ μ œκ³΅ν•˜λŠ” λͺ¨λ“  μ‚¬λžŒμ—κ²Œ κ°μ‚¬ν•©λ‹ˆλ‹€.

@up209d 예, μ†”λ£¨μ…˜μ΄ μž‘λ™ν•˜μ§€λ§Œ
styles = { name: { cssprop: props => {} } ν‘œκΈ°λ²•, μ•„λ‹˜
styles = props => ({ name: { cssprop: {} })

λ˜ν•œ JssProviderλŠ” ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

@koutsenko

// at value level:
styles = { name: { cssprop: props => value }
styles = theme => ({ name: { cssprop: props => value })

// at class name level
styles = { name: props => ({ cssprop: value })  }
styles = theme => ({ name: props => ({ cssprop: value })  })

theme λ‹€μŒμ˜ 두 번째 μΈμˆ˜λ‘œλ„ μ΅œμƒμœ„ μˆ˜μ€€μ—μ„œ props 에 μ•‘μ„ΈμŠ€ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

방법을 μ°Ύμ•˜μ–΄μš”

// MyComponent.tsx
import React, { PureComponent } from 'react';
import { myComponentWithStyles } from './myComponentWithStyles';

export interface MyComponentProps {
  copy: string;
  size?: number;
}

export class Twemoji extends PureComponent<myComponentWithStyles> {
  public render() {
    const { copy, classes } = this.props;

    return (
      <div className={classes.message}>
        {copy}
        <img src="https://via.placeholder.com/150" />
    </div>
    );
  }
}

// myComponentWithStyles.tsx
import React from 'react';
import { withStyles, WithStyles, Theme } from '@material-ui/core';
import { MyComponent, MyComponentProps } from './my-component';

const styles = (props: Theme & MyComponentProps) => ({
  message: {
    fontSize: props.typography.caption.fontSize,
    'box-sizing': 'content-box',
    '& img': {
      width: `${props.size || 24}px`,
      height: `${props.size || 24}px`,
      padding: '0 4px',
      verticalAlign: 'middle',
    },
  },
});

export type myComponentWithStyles = WithStyles<any>;

export const Component = (props: MyComponentProps) => {
  const StyledComponent = withStyles((theme: Theme) => styles({ ...props, ...theme }))(
    MyComponent
  );

  return <StyledComponent {...props} />;
};



md5-d0e1b51e375682cf2aad9c4d66b6c73a



<Component size={12} />

@andreasonny83 이 νŒ¨ν„΄μ„ ν”Όν•˜μ„Έμš”. v4μ—μ„œ κΈ°λ³Έ APIλ₯Ό μ œκ³΅ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

@oliviertassinari μ—…λ°μ΄νŠΈμ— κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€. κ·Έ νŒ¨ν„΄μ€ 이미 μ‚¬μš© κ°€λŠ₯ν•œκ°€μš”? μ‚¬μš© κ°€λŠ₯ν•œ λ¬Έμ„œκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

λ§ˆμ§€λ§‰ 질문 @oliviertassinari . makeStyles λ₯Ό withStyles 와 ν•¨κ»˜ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

그에 λŒ€ν•œ λ¬Έμ„œλ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€. λ‚΄κ°€ν•˜λ €κ³ ν•˜λŠ” 것은 μ΄κ²ƒμž…λ‹ˆλ‹€ :

const useStyles = makeStyles({
  message: {
    boxSizing: 'content-box'
  }
});

export const ComponentWithStyles = withStyles(useStyles())(MyComponent);

@andreasonny83

  • makeStyles λŠ” λ°˜μ‘ ν›„ν¬μš©μž…λ‹ˆλ‹€.
  • withStyles HOCλŠ” λ™μΌν•œ ꡬ문을 ν—ˆμš©ν•©λ‹ˆλ‹€.

κ·€ν•˜μ˜ μ˜ˆμ—μ„œ makeStyles μ œκ±°ν•˜μ‹­μ‹œμ˜€.

const styles = { message: {boxSizing: 'content-box', background: props => props.bg} };
export const ComponentWithStyles = withStyles(styles)(MyComponent);

Gday μ‚¬λžŒλ“€μ€ μœ„μ˜ 토둠을 μ°Έμ‘°ν•˜μ—¬ ν˜„μž¬ μ†”λ£¨μ…˜μ„ κ³΅μœ ν•œλ‹€κ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. λˆ„κ΅°κ°€ λ˜λŠ” λˆ„κ΅°κ°€κ°€ ν˜„μž¬ μ†”λ£¨μ…˜μ— λŒ€ν•΄ 더 λ‚˜μ€ 쑰언을 μ œκ³΅ν•  수 있기λ₯Ό λ°”λžλ‹ˆλ‹€. μž„μ˜μ˜ λ°°κ²½ 이미지와 같은 λ‚΄ 둜그인 νŽ˜μ΄μ§€ ID의 경우 IDλŠ” μ—¬μ „νžˆ 재료 UI API의 νž˜μ„ μœ μ§€ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. AuthPageλŠ” κ°œλ³„ 인증 ꡬ성 μš”μ†Œ(둜그인, 잠금, λΉ„λ°€λ²ˆν˜Έ λΆ„μ‹€, λΉ„λ°€λ²ˆν˜Έ μž¬μ„€μ • λ“±)λ₯Ό μžμ‹μœΌλ‘œ μ‚¬μš©ν•˜λŠ” λΆ€λͺ¨ ν”„λ ˆμ  ν…Œμ΄μ…˜ λ ˆμ΄μ–΄μΌ λΏμž…λ‹ˆλ‹€. 각 νŽ˜μ΄μ§€λ₯Ό μƒˆλ‘œ κ³ μΉ  λ•Œλ§ˆλ‹€ μƒˆλ‘œμš΄ λ°±κ·ΈλΌμš΄λ“œ λ‘œλ“œμ™€ AuthPageContainer prop λ‚΄μ˜ κ°•λ ₯ν•œ ν˜•μ‹μ˜ propsλ₯Ό 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

// μΈμ¦νŽ˜μ΄μ§€.styles.tsx

import { Container } from "@material-ui/core";
import { ContainerProps } from "@material-ui/core/Container";
import { withStyles } from "@material-ui/core/styles";
import React from "react";

interface IAuthContainerProps extends ContainerProps {
  background: string;
}

export const AuthContainer = withStyles({
  root: props => ({
    alignItems: "center",
    backgroundImage: `url(${props.background})`,
    backgroundPosition: "50% 50%",
    backgroundRepeat: "no-repeat",
    backgroundSize: "cover",
    display: "flex",
    height: "100vh",
    justifyContent: "center",
    margin: 0,
    padding: 0,
    width: "100%"
  })
})((props: IAuthContainerProps) => <Container maxWidth={false} {...props} />);

// μΈμ¦νŽ˜μ΄μ§€.tsx

import React from "react";
import forest from "../../assets/backgrounds/forest.jpg";
import sky from "../../assets/backgrounds/sky.jpg";
import uluru from "../../assets/backgrounds/uluru.jpg";
import { AuthContainer } from "./AuthPage.styles";

const AuthPage = ({ children }) => {
  const generateBackground = () => {
    const backgrounds = [forest, sky, uluru];
    const index = Math.floor(Math.random() * backgrounds.length);
    return backgrounds[index];
  };

  return (
    <AuthContainer background={generateBackground()}>{children}</AuthContainer>
  );
};

export default AuthPage;

κ°„λ‹¨νžˆ λ‹€μŒκ³Ό 같이 ν•˜μ‹­μ‹œμ˜€.

// styles.js
export default theme => ({
    root: props => ({
        // some styles
    }),
    ...
});

//container.js
export default withStyles(styles)(MyComponent);

λ˜ν•œ μƒνƒœλ₯Ό μ „λ‹¬ν•˜λŠ” 것은 μ–΄λ–»μŠ΅λ‹ˆκΉŒ?

@luky1984
당신은 ν•  수 μ—†μŠ΅λ‹ˆλ‹€. λŒ€μ‹  λ‹€μŒμ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

// Component.js
<Button
    className={`
        ${classes.button} 
        ${this.state.isEnable
            ? classes.enable
            : classes.disable}
    `}
/>

λ˜λŠ” λŒ€μ‹  clsx https://www.npmjs.com/package/clsxλ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

@caub κ·€ν•˜μ˜ μ†”λ£¨μ…˜μ€ jss 생성 클래슀 이름 μˆœμ„œλ₯Ό
여기에 μž‘μ„±λœ κ²ƒμ²˜λŸΌ: https://github.com/mui-org/material-ui/issues/8726#issuecomment -363546636
κ·€ν•˜μ˜ μ†”λ£¨μ…˜μ„ μ‚¬μš©ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ κ·€ν•˜μ˜ HOC ꡬ성 μš”μ†Œ(withStylesProps) μ†”λ£¨μ…˜μ€ λž˜ν•‘λ˜μ–΄ 있기 λ•Œλ¬Έμ— withStyles의 ν˜ΈμΆœμ„ μ§€μ—°μ‹œν‚€λ―€λ‘œ classNames둜 ν˜ΈμΆœν•΄λ„ CSSκ°€ μž¬μ •μ˜λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
μ’‹μ•„μš”: https://codesandbox.io/s/hocs-8uhw1?file=/index.js

배경은 #0000000이고 색상은 νŒŒλž€μƒ‰μ΄μ–΄μ•Ό ν•©λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰