Material-ui: theme.spacing은 @ material-ui / [styles / core] 4.0.1의 ν•¨μˆ˜κ°€ μ•„λ‹™λ‹ˆλ‹€.

에 λ§Œλ“  2019λ…„ 05μ›” 31일  Β·  18μ½”λ©˜νŠΈ  Β·  좜처: mui-org/material-ui

였늘 λ‹€λ₯Έ 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ 전체 Material-UI 4.0.0 μž‘μ—… 곡간을 μž¬μ„€μ •ν•˜μ—¬ 코어와 μŠ€νƒ€μΌμ„ λͺ¨λ‘ μ΅œμ‹  4.0.1둜 λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ν•˜λ €κ³ ν–ˆμŠ΅λ‹ˆλ‹€.

ThemeProvider λ₯Ό μ‚¬μš©ν•˜λ €κ³ ν•˜λ©΄ TypeError theme.spacing is not a function λ°›μŠ΅λ‹ˆλ‹€.

  • [x] 이것은 v0.x λ¬Έμ œκ°€ μ•„λ‹™λ‹ˆλ‹€.
  • [x]이 μ €μž₯μ†Œμ˜ 문제 λ₯Ό κ²€μƒ‰ν–ˆμœΌλ©° 이것이 쀑볡이 μ•„λ‹ˆλΌκ³  μƒκ°ν•©λ‹ˆλ‹€. κΈ€μŽ„ ... μ •ν™•νžˆ ... https://github.com/mui-org/material-ui/issues/15834 μ—μ„œ 쀑볡을 μ°Ύμ•˜μ§€λ§Œ μˆ˜μ •μ΄ μž‘λ™ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. λ‚˜μœ μ‚¬μš©λ²•μ΄ ν•΅μ‹¬μ—μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

μ˜ˆμƒλ˜λŠ” λ™μž‘ πŸ€”

ν…Œλ§ˆκ°€ μž‘λ™ν•˜λŠ” 것을보고 μ‹ΆμŠ΅λ‹ˆλ‹€. theme.spacing 이 λ§ˆμŠ€ν„° λΈŒλžœμΉ˜μ™€ λ‹€μŒ 브랜치 λͺ¨λ‘μ—μ„œ μ‚¬μš©λœλ‹€λŠ” 사싀에 ν˜Όλž€μŠ€λŸ¬μ›Œμ„œ 이것이 갈 길인 것 κ°™μŠ΅λ‹ˆλ‹€.

ν˜„μž¬ 행동 😯

μœ„μ˜ 였λ₯˜ μ°Έμ‘°

μž¬ν˜„ 단계 πŸ•Ή

링크 : https://codesandbox.io/s/materialui-style-issue-rk8n0

μ»¨ν…μŠ€νŠΈ πŸ”¦

μ™„μ „νžˆ μƒˆλ‘œμš΄ μ‘μš© ν”„λ‘œκ·Έλž¨μ„ μ‹œμž‘ν–ˆκ³  Material-UI의 λͺ¨λ“  μŠ€νƒ€μΌ κΈ°λŠ₯을 ν•¨κ»˜ νŒŒμ•…ν•˜λ €κ³ ν•©λ‹ˆλ‹€. 즉, style λͺ¨λ“ˆμ˜ 4.0.1둜 λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ν•˜κΈ° μ „μ—λŠ” 그런 λ¬Έμ œκ°€ μ—†μ—ˆκΈ° λ•Œλ¬Έμ— 4.x λ¦΄λ¦¬μŠ€κ°€ λΆˆμ•ˆμ •ν•œ 지 λ˜λŠ” 무엇인지 ν™•μ‹€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‹Ήμ‹ μ˜ ν™˜κ²½ 🌎

| 기술 | 버전 |
| -------------- | --------- |
| Material-UI | v4.0.1 |
| λ°˜μ‘ | 16.0.6 |
| λΈŒλΌμš°μ € | FF / Chrome μ΅œμ‹  |
| TypeScript | -|

question

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

문제λ₯Ό λ°œκ²¬ν•΄μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€.
import { withStyles } from '@material-ui/styles'; λ₯Ό import { withStyles } from '@material-ui/core/styles'; 둜 λ³€κ²½ν•˜λ©΄ λͺ¨λ“  것이 잘 μž‘λ™ν•©λ‹ˆλ‹€.
감사!

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

@keul 코어와 μŠ€νƒ€μΌμ˜ 차이점에 https://material-ui.com/customization/default-theme/#material -ui-core-styles-vs-material-ui-styles.

κ°μ‚¬ν•©λ‹ˆλ‹€ @oliviertassinari.

Material-UI μŠ€νƒ€μΌμ€ @ material-ui / styles npm νŒ¨ν‚€μ§€λ‘œ κ΅¬λ™λ©λ‹ˆλ‹€. Reactλ₯Όμœ„ν•œ μŠ€νƒ€μΌλ§ μ†”λ£¨μ…˜μž…λ‹ˆλ‹€. 이 μ†”λ£¨μ…˜μ€ κ²©λ¦¬λ˜μ–΄ 있으며 κΈ°λ³Έ Material-UI ν…Œλ§ˆμ— λŒ€ν•œ 지식이 μ—†μŠ΅λ‹ˆλ‹€. React의 μ»¨ν…μŠ€νŠΈμ— ν…Œλ§ˆλ₯Ό μ²΄κ³„μ μœΌλ‘œ μ‚½μž… ν•  ν•„μš”λ₯Ό μ—†μ• κΈ° μœ„ν•΄ μŠ€νƒ€μΌ λͺ¨λ“ˆ (makeStyles, withStyles 및 styled)을 κΈ°λ³Έ Material-UI ν…Œλ§ˆλ‘œ λž˜ν•‘ν•©λ‹ˆλ‹€.

μ†”μ§νžˆ λ§ν•΄μ„œ μ—¬κΈ°μ„œ μš”μ μ„ μ΄ν•΄ν•˜μ§€ λͺ»ν•˜λ©° 이것이 λ¬Έμ œμ™€ μ–΄λ–»κ²Œ κ΄€λ ¨λ˜μ–΄ μžˆλŠ”μ§€ μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€. 즉, core/styles μ—μ„œ κ°€μ Έμ˜¬ λ•Œ 이미 배솑 λ˜μ—ˆκΈ° λ•Œλ¬Έμ— ThemeProviderκ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

μΆ”κ°€ μ»¨ν…μŠ€νŠΈλ₯Ό μ œκ³΅ν•˜κΈ° μœ„ν•΄ λ‚΄ μ½”λ“œλŠ” core 4.0.0 및 styles 3.0.0-alpha.10을 μ‚¬μš©ν•˜μ—¬ μž‘λ™ν–ˆμŠ΅λ‹ˆλ‹€.
4.0.1둜 λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ ν•œ ν›„ 였늘 아침에 λ¬Έμ œκ°€ λ°œμƒν•˜κΈ° μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€.

이전 νŒ¨ν‚€μ§€ 잠금 λ²„μ „μœΌλ‘œ λΉ λ₯΄κ²Œ λŒμ•„κ°ˆ 수 μžˆμ§€λ§Œ 큰 그림을 μ–»κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

@keul μ’‹μ•„, μ•„λ§ˆλ„ λ‹€μŒμ΄ 더 λͺ…ν™•ν•΄μ§ˆ κ²ƒμž…λ‹ˆλ‹€. 핡심 ꡬ성 μš”μ†Œμ™€ ν˜Έν™˜λ˜μ§€ μ•ŠλŠ” μƒˆ ν…Œλ§ˆλ₯Ό μ‚½μž…ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. createMuiThemeλ₯Ό μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€.

import { createMuiTheme } from "@material-ui/core";

const theme = createMuiTheme({
  spacing: 4,
  palette: {
    primary: {
      main: "#007bff",
    },
  }
});

@oliviertassinari 음 ... κ°μ‚¬ν•©λ‹ˆλ‹€. 이것은 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

λ¬Έμ„œμ—μ„œμ΄ μ›μ‹œ μ½”λ“œλ₯Ό λ³΅μ‚¬ν–ˆλ‹€κ³  ν™•μ‹ ν•©λ‹ˆλ‹€ . https://material-ui.com/styles/advanced/ μ—μ„œ 찾은 것과 λ™μΌν•œ 방법

μ†ŒμŒ μ£„μ†‘ν•©λ‹ˆλ‹€!

https://material-ui.com/styles/x의 λͺ¨λ“  것은 머티리얼 λ””μžμΈκ³Ό 관련이 μ—†μŠ΅λ‹ˆλ‹€. Reactλ₯Όμœ„ν•œ μŠ€νƒ€μΌλ§ μ†”λ£¨μ…˜μž…λ‹ˆλ‹€.

μ£„μ†‘ν•©λ‹ˆλ‹€. μ—¬μ „νžˆ 이해할 수 μ—†μŠ΅λ‹ˆλ‹€.
μš°λ¦¬λŠ” 보낼 수 theme μŠ€νƒ€μΌμ— 인수둜, κ·Έλ¦¬κ³ ν•˜μ—¬ 포μž₯ withstyles ? λ‹€μŒκ³Ό 같이.

const styles = theme => {({
    root: {
      // JSS uses px as the default units for this CSS property.
      padding: theme.spacing(2), // = 8 * 2
    },
})};

ν…Œλ§ˆλ₯Ό μŠ€νƒ€μΌμ— λŒ€ν•œ 인수둜 보내고 withstyles둜 λž˜ν•‘ ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? λ‹€μŒκ³Ό 같이.

@ByronHsu 예, @material-ui/core/styles μ—μ„œ κ°€μ Έ μ˜€κ±°λ‚˜ createMuiTheme() λ§Œλ“  ν…Œλ§ˆλ₯Ό μ‚½μž…ν•˜λŠ” κ²½μš°μž…λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ theme.spacing λ°˜ν™˜ν•˜λŠ” 것은 ν•¨μˆ˜κ°€ μ•„λ‹™λ‹ˆλ‹€.

@ByronHsu λ³΅μ œν’ˆμ΄ μžˆμŠ΅λ‹ˆκΉŒ?

문제λ₯Ό λ°œκ²¬ν•΄μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€.
import { withStyles } from '@material-ui/styles'; λ₯Ό import { withStyles } from '@material-ui/core/styles'; 둜 λ³€κ²½ν•˜λ©΄ λͺ¨λ“  것이 잘 μž‘λ™ν•©λ‹ˆλ‹€.
감사!

이 λ¬Έμ œλ„ λ°œμƒν•˜κ³  있으며

import { makeStyles, Theme } from "@material-ui/core/styles";

const useStyles = makeStyles((theme: Theme) => ({
  icon: {
    marginRight: theme.spacing(1)
  }
}));

이 ꡬ성 μš”μ†ŒλŠ” <ThemeProvider /> λž˜ν•‘λ©λ‹ˆλ‹€.

λŒ€μ‹  μ‚¬μš©ν•˜λ©΄ λ™μΌν•œ λ¬Έμ œκ°€ λ°œμƒν•©λ‹ˆλ‹€.

import { makeStyles } from "@material-ui/styles";
import { Theme } from "@material-ui/core/styles";

const useStyles = makeStyles((theme: Theme) => ({
  icon: {
    marginRight: theme.spacing(1)
  }
}));

λ‚˜λŠ” μ–»λ‹€:

TypeError: theme.spacing is not a function

λͺ¨λ“  MUI νŒ¨ν‚€μ§€λŠ” μ΅œμ‹  λ§ˆμŠ€ν„° λΈŒλžœμΉ˜μž…λ‹ˆλ‹€.

μ‹ κ²½ 쓰지 마, 신인 μ‹€μˆ˜. μƒˆ ν…Œλ§ˆλ₯Ό createMuiTheme 둜 κ°μ‹ΈλŠ” 것을 μžŠμ—ˆμŠ΅λ‹ˆλ‹€. λŒ€μ‹  κΈ°λ³Έ 개체λ₯Ό λ°˜ν™˜ν–ˆμŠ΅λ‹ˆλ‹€.

여기도 κ°™λ‹€

μ™ΈλΆ€ ν…Œλ§ˆκ°€ 없을 λ•Œ μ½”λ“œλ₯Ό μ œμ™Έν•˜κ³  μ˜ˆμƒλŒ€λ‘œ λͺ¨λ“  μž‘μ—…μ„ μˆ˜ν–‰ν•˜λ©΄μ΄ λ¬Έμ œκ°€ λ‚˜νƒ€λ‚©λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄ :

νœ΄μ‹ :

<MuiThemeProvider theme={outer => ({...outer, ...theme})}>...</MuiThemeProvider>

곡μž₯:

<MuiThemeProvider>
  <MuiThemeProvider theme={outer => ({...outer, ...theme})}>...</MuiThemeProvider>
</MuiThemeProvider>

이 사건에 λŒ€ν•œ κ²½κ³ κ°€ μžˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜λ„ μ—¬μ „νžˆ μž‘λ™ν•˜κ±°λ‚˜ 적어도 ν•΄λ‹Ή 논리λ₯Ό 제곡된 ν•¨μˆ˜μ— 남겨 두어야 ν•  것 κ°™μŠ΅λ‹ˆλ‹€.

μ—μ„œ 이동
marginRight: theme.spacing(1)

그것을 λ³€κ²½
marginRight: theme.spacing.unit

이것은 λ‚˜μ—κ²Œλ„ λ¬Έμ œμž…λ‹ˆλ‹€. κ°’ 자체λ₯Ό λ‘œκΉ…ν•˜κ³  있고 μž‘λ™ 쀑이기 λ•Œλ¬Έμ— μœ ν˜• 였λ₯˜ 인 것 κ°™μ§€λ§Œ next.jsλŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 였λ₯˜λ₯Ό λ˜μ§‘λ‹ˆλ‹€. 쀑단 점도 μžˆμŠ΅λ‹ˆλ‹€.

문제λ₯Ό λ°œκ²¬ν•΄μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€.
import { withStyles } from '@material-ui/styles'; λ₯Ό import { withStyles } from '@material-ui/core/styles'; 둜 λ³€κ²½ν•˜λ©΄ λͺ¨λ“  것이 잘 μž‘λ™ν•©λ‹ˆλ‹€.
감사!

κ°μ‚¬ν•©λ‹ˆλ‹€ @ByronHsu

κ·Έκ²ƒμ˜ μž‘λ™

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