typescriptμμ withStyles()
hocλ₯Ό μ¬μ©ν λ λ°νλ κ΅¬μ± μμλ₯Ό μ¬μ©νλ €κ³ ν λ λ€μ μ€λ₯κ° λ°μν©λλ€.
Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<App> & Readonly<{ children?: ReactNode; }> & Reado...'.
Type '{}' is not assignable to type 'Readonly<WithStyles<"main">>'.
Property 'classes' is missing in type '{}'.
μ ν μ μμ λν μ΄λ¬ν λ³κ²½ μ μ΄ λ¬Έμ μ κ΄λ ¨μ΄ μμ μ μμ΅λλ€.
μλμ App
κ΅¬μ± μμ μ½λκ° μ£Όμ΄μ§λ©΄ 1.0.0-beta.10μμ νλ κ²μ²λΌ μ ν μ€λ₯ μμ΄ κ΅¬μ± μμ <App />
λ₯Ό μ¬μ©ν μ μμ΄μΌ ν©λλ€.
μλμ App
κ΅¬μ± μμ μ½λμμ <App />
λ₯Ό μ¬μ©νλ €κ³ νλ©΄ μμ μΈκΈν μ€λ₯κ° λ°μν©λλ€.
import * as React from 'react';
import { withStyles } from 'material-ui/styles';
const styles = {
main: {
marginTop: 48,
padding: 10,
},
foo: {
margin: 0,
},
};
interface Props {
message: string;
};
type ClassNames = { classes: { [className in keyof typeof styles]: string } };
class App extends React.Component<Props & ClassNames> {
render() {
const { classes, message } = this.props;
return (
<div className={classes.main}>
<div className={classes.foo} >
Hello World! {message}
</div>
</div>
);
}
}
export default withStyles(styles)(App);
μ½λλ 1.0.0-beta.10μμ μ λλ‘ μλνμ§λ§ 1.0.0-beta.12λ‘ μ κ·Έλ μ΄λνμ λ μ ν μ€λ₯κ° λ°μνμ΅λλ€.
μ 곡λ μ½λ μ‘°κ°μμ ν΄λμ€ μ΄λ¦ λͺ©λ‘μ λ λ² μ μν νμκ° μλλ‘ keyof typeof styles
νΈλ¦μ μ¬μ©νμ΅λλ€(λ°λ³΅μ±μ λ§€μ° μ«μ΄ν©λλ€). λλ λν λ€λ₯Έ λ³νμ μλνμ΅λλ€:
type ClassNames = WithStyles<keyof typeof styles>;
λ μΌλ°μ μΈ λ°©λ²μΌλ‘ μνν©λλ€( styles.spec.tsx μμ λ³Ό μ
type ComponentClassNames = 'main' | 'foo';
type ClassNames = WithStyles<ComponentClassNames>;
μ¬μ ν κ°μ μ€λ₯κ° λ°μν©λλ€.
μ΄μ μ ν μ μλ props μ νμ΄ classes
μ΅μ
μ΄ μλ StyledComponentProps
μΈ κ΅¬μ± μμλ₯Ό λ°ννλ κ² κ°μ΅λλ€. μλ‘μ΄ μ μ...
<P, C extends React.ComponentClass<P & StyledComponentProps<Names>>>(
component: C
): C;
...κ΅¬μ± μμμ λμΌν μ ν C
μ λ°νν©λλ€. μ¦, μ ν μ¬νμΌλ‘ νμλμ§ μμ ClassNames
λ₯Ό μ λ¬νλ©΄ λ°νλ κ΅¬μ± μμμ μ νλ©λλ€. λ΄κ° μΈκΈμ μ°Έμ‘°νμμμ€ μ¬κΈ°μ μ¬μ© Partial<>
λ΄κ°λ³΄κΈ° νν ν΄νΉ μκ°ν©λλ€.
| κΈ°μ | λ²μ |
|----------------|---------|
| λ¨Έν°λ¦¬μΌ UI | 1.0.0-λ² ν.12 |
| λ°μ | 15.6.1 |
| λΈλΌμ°μ | ν¬λ‘¬ 61.0.3163.100 |
@cfilipov λΉμ μ΄ μ¬μ©νλ κ΅¬μ± μμ ν΄λμ€λ₯Όνλ€ μ₯μ ν λ StyledComponentProps
λμ WithStyles
, κ·Έλ¦¬κ³ μ격ν νμ
κ²μ¬ λͺ¨λμμ λΉμ μ nullμ΄ μλ μ£Όμ₯ μ°μ°μλ₯Ό μ¬μ©ν΄μΌ ν κ²½μ° !
μΆμΆλ¬Όμμ classes
μ νλ μ΄κ²μ withStyles
λ₯Ό ν΄λμ€ λ°μ½λ μ΄ν°λ‘ μ¬μ©ν μ μλλ‘ νλ μ μΆ©μμ
λλ€. TypeScriptμ ν΄λμ€ λ°μ½λ μ΄ν°λ λ°ν μ νμ΄ μΈμ μ νκ³Ό μΌμΉν΄μΌ νλ€λ μ ν μ΄ μμ΅λλ€. μ΄ μ ν μ¬νμ΄ μλ κ²½μ° ν κ°μ§ μ΅μ
λ§ κ°λ₯ν©λλ€.
withStyles
λ₯Ό ν΄λμ€ λ°μ½λ μ΄ν°λ‘ μ¬μ©νλ κ²μ μ§μνμ§ μμ΅λλ€.classes
μνμ μ λ¬νλλ‘ μꡬν©λλ€(μ΄μ μ μΆ©μμ νλ¦Όμμ΄ λ μ§μ¦λ¬μ )classes
κ° κ΅¬μ± μμ λ΄μμ nullableλ‘ κ°μ£Όλλλ‘ μꡬνμ¬ ν΄λΉ νλμ μ‘μΈμ€ν λ !
μ°μ°μλ₯Ό μ¬μ©ν΄μΌ ν©λλ€( νμ¬ νν ).κ΅¬μ± μμμ μνκ° μλ κ²½μ° μν λΉμ μ₯ κΈ°λ₯ κ΅¬μ± μμλ₯Ό μ¬μ©νλ κ²μ΄ μ’μ΅λλ€. κ·Έλ¬λ©΄ μ ν μ£Όμμ΄ λ μ κ² νμνκ³ μ νμ΄ λ μμ ν©λλ€.
export default withStyles(styles)<Props>(({ classes, message }) => (
<div className={classes.main}>
<div className={classes.foo}>
Hello World! {message}
</div>
</div>
));
λ΄κ° μΈκΈμ μ°Έμ‘°νμμμ€ μ¬κΈ°μ λ΄κ°λ³΄κΈ° νν ν΄νΉ μκ°νλ <> λΆλΆμ μ¬μ©μ.
ν΄λΉ λκΈμ λν λ΄ νμ μ‘°μΉλ₯Ό μ½μΌλ©΄ Partial<>
κ° νμνμ§ μλ€λ κ²μ μ μ μμ΅λλ€.
ν΄λΉ λκΈμ λν λ΄ νμ μ‘°μΉλ₯Ό μ½μΌλ©΄ Partial<>μ΄ νμνμ§ μλ€λ κ²μ μ μ μμ΅λλ€.
Partial<>
μ λν κ·νμ μ견μ 보μμ΅λλ€. StyledComponentProps
λ₯Ό μ¬μ©νλ κ²μ λ³Έμ§μ μΌλ‘ λμΌν κ²κ³Ό κ°μ΅λλ€(ν΄λΉ μ μμμ Partial<>
νκ³ μμ). λ΄ λΆλ§μ μ΄μ ν΄λμ€ μ΄λ¦μ !
λ‘ μ‘μΈμ€ν΄μΌ νλ€λ κ²μ
λλ€(λΉμ μ΄ μΈκΈνλ―μ΄). λλ―Έ classes
μνμ μ λ¬νκ±°λ !
μ¬μ©νλλ‘ μꡬνλ κ²μ λ λ€ μ’μ§ μμ μ μΆ©μμ΄λΌκ³ μκ°ν©λλ€.
λͺ νν νμλ©΄, μ¬κΈ°μ μ κ° κ²ͺκ³ μλ κ·Όλ³Έμ μΈ λ¬Έμ λ typescriptμ μ€νμ κΈ°λ₯ μ νκ³λ₯Ό ν΄κ²°νκΈ° μν λ Έλ ₯μΌλ‘ μμμ νκ·λ₯Ό λμ νλ μ νμ΄λΌκ³ μκ°ν©λλ€. λλ λ°μ½λ μ΄ν°μ κ΄μ¬μ΄ μκΈ° λλ¬Έμ μ½κ°μ νΈκ²¬μ μΈμ νμ§λ§ λ€λ₯Έ μ¬λλ€μ λΆλͺ ν μ κ²½ μ°μ§λ§ μ¬κΈ°μμμ μ μΆ©μμ λμκ² μλ―Έκ° μμ΅λλ€.
@cfilipov withStyles
νμ΄νμ 첫 λ²μ§Έ 리ν©ν° λ μ΅μ
(1)μ μ ννλ κ²μ΄μμ΅λλ€. μ¦, withStyles
λ₯Ό ν΄λμ€ λ°μ½λ μ΄ν°λ‘ μ¬μ©νλ λμ ν΄λμ€ λ° ν¨μ κ΅¬μ± μμ λͺ¨λμ λν΄ νμ΄νμ μμ ν μ¬λ°λ₯΄κ² λ§λλ κ²μ
λλ€. . κ·Έλ° λ€μ λ°μ½λ μ΄ν° ꡬ문μ μ¬μ©νλ κ²μ΄ μΈκΈ° μλ μμ²μ΄λΌλ νΌλλ°± μ λ°μ μ΅μ
(3)μΌλ‘ μ ννμ΅λλ€. κ·Έ κ²°μ μ λ€μ λ³΄κ² λμ΄ κΈ°μ©λλ€. λλ νμ¬ λ°κΈ°λ₯ μνμμ λ°μ½λ μ΄ν°λ₯Ό μ§μνλ κ²λ³΄λ€ μ ν μμ μ±μ μ νΈν©λλ€.
λ€, μ΄λ° μΈκΈ° μλ μμ²μ μ§μνκ³ μΆμ λ§μμ μ΄ν΄ν©λλ€. λλ λ°μ½λ μ΄ν°λ₯Ό μ¬μ© νκ³ μΆμ§λ§ (mui μΈλΆ) ν λλ§λ€ λ°μ½λ μ΄ν°μ κ΄λ ¨λ λ§μ λ¬Έμ κ° κ³μ λ°μνλ―λ‘ κ°μΈμ μΌλ‘ μ€λΉλ λκΉμ§ μ¬μ©νμ§ μκΈ°λ‘ κ²°μ νμ΅λλ€.
λΉμ μ΄ λ΄ κ΄μ¬μ¬λ₯Ό 곡μ νλ κ²μ²λΌ λ€λ¦¬κ³ λ μ΄μ μΆκ°ν κ²μ΄ μμΌλ―λ‘ λ€λ₯Έ μ¬λλ€μ΄ μ΄ λ°©ν₯μΌλ‘ νΌλλ°±μ μ 곡νμ¬ μν₯μ μ€ μ μκΈ°λ₯Ό λ°λλλ€.
λλ beta.10μμ beta.13μΌλ‘ μ ννμ¬ λ³κ²½λ μ¬νμ΄ μλμ§ νμΈνμΌλ©° μ, μ΄κ²μ μ€μ λ¬Έμ μ λλ€. μ¬κΈ°μ λ΄ 2μΌνΈλ₯Ό λμ§κΈ° μν΄ λ°μ½λ μ΄ν°λ μ€νμ μ λλ€. κ·Έλ€μ λΆλͺ ν λ―Έλμ λ°λ μ μμ΅λλ€. κ·Έλ¦¬κ³ κ·ΈλκΉμ§ 100% μ νν λ°©λ²μ μ μ μΌλ‘ μ§μ§ν©λλ€. λλ μΌμ μλμν€κΈ° μν΄ λ΄ μ νμ ν΄νΉνλ λ° μΌκ΄μ± μλ μ ν μμ μ ν¨μ¬ λ μ νΈν©λλ€.
@withStyles()
λ₯Ό (TypeScriptμμ) λ°μ½λ μ΄ν°λ‘ μ§μνμ§ μλ κ²μ κ³ λ €ν΄μΌ ν©λλ€.νμ΄νμ μ¬λ°λ₯΄κ² νλ€λ©΄ ν΄λμ€λ₯Ό "μ₯μ"νλ λͺ¨μ΅μ λ€μκ³Ό κ°μ΅λλ€.
type NonStyleProps = {
text: string
};
const styles = {
root: {
backgroundColor: 'red'
}
};
const DecoratedComponent = withStyles(styles)(
class extends React.Component<NonStyleProps & WithStyles<'root'>> {
render() {
return (
<div className={this.props.classes.root}>
{this.props.text}
</div>
);
}
}
);
@pelotom μ΄ λ¬Έμ λλ¬Έμ μμ§ beta.10μμ μμΌλ‘ λμκ°μ§ λͺ»νκ³ μμ΅λλ€. μ΄μ μ Reduxμ connect λ©μλλ₯Ό μ¬μ©ν ν΄λμ€ μ€νμΌμ λν΄ μΈκΈν μ μ΄ μμ΅λλ€. μλμ μΌλ‘ μ½κ³ κ°λ ₯νλ€κ³ μκ°ν©λλ€. #8059μμ μΈ λ²μ§Έ μ£Όμμ μ νμ λν λ μμ μ λλ€.
@pelotom μ΄ λ¬Έμ μ λν΄ μλ €μ£Όμ
μ κ°μ¬ν©λλ€. μ λ TS λ°μ½λ μ΄ν°λ₯Ό λ§μ΄ μ¬μ©νμ§λ§, κ·Έλ° κ²½μ°μλ withStyles
κ° λ λμ μ ν μμ μ±μ μν΄ λ°μ½λ μ΄ν
μ§μμ μ€λ¨νκ² λμ΄ κΈ°μ©λλ€.
@withStyle()μ (TypeScriptμμ) λ°μ½λ μ΄ν°λ‘ μ§μνμ§ μλ κ²μ κ³ λ €ν΄μΌ ν©λλ€.
@pelotom μ λ κ°μΈμ μΌλ‘ μ΄ λ³νμ μ°¬μ±ν©λλ€. @sebald μ¬κΈ°μ 무μμ νκ³
κ°λ¨ν λ³κ²½μ λλ€. νΉμλΌλ ν¨κ» κ°μ€ λΆλ€μ μν΄ PRμ μ΄μμ΄μ π
@pelotom μ΄λ κ² μ¬μ©νλ©΄ νμ΄νμ΄ κ³μ μλν κΉμ?
interface IStyles {
// styles interface
}
interface IHeaderInfoStyles {
classes: any;
}
interface IHeaderInfoProps {
// properties resulting from dispatches
}
interface IHeaderInfoInjectedProps {
// props injected from parent if any
}
interface IHeaderInfoDispatchProps {
// dispatches
}
interface IHeaderInfoState {
// your state for the class
}
type HeaderInfoProps = IHeaderInfoProps & IHeaderInfoInjectedProps & IHeaderInfoDispatchProps & IHeaderInfoStyles;
class HeaderInfo extends Component<HeaderInfoProps, IHeaderInfoState> {
export const HeaderInfo_container = withStyles<IHeaderInfoInjectedProps, IStyles>(styles)(
connect<IHeaderInfoProps, IHeaderInfoDispatchProps, (IHeaderInfoInjectedProps & IHeaderInfoStyles)>(mapStateToProps, mapDispatchToProps)(HeaderInfo),
);
@marcusjwhelan withStyles
λ λ μ΄μ 2κ°μ μ ν 맀κ°λ³μλ₯Ό μ¬μ©νμ§ μμΌλ©° μ€νμΌμ λν μ ν 맀κ°λ³μλ₯Ό μ 곡ν νμκ° μμ΅λλ€( styles
μμ μ μΆν μ μμ). λμ μΈ μ μμ΄μΌ ν©λλ€.
withStyles(styles)<NonStyleProps>(...);
mapStateToProps
λ° mapDispatchToProps
λν μ νμ μ 곡νλ©΄ κ·νμ μμμ μ΄κ²μ΄ μ΄λ»κ² 보μ΄λμ§ λ³΄μ¬μ€ μ μμ΅λλ€.
ν; λ°μ¬; λ³κ²½μ νκ³ λ°λ°μ΄ μ΄λ€μ§ λ΄ μλ€ π·π»
@oliviertassinari @pelotom Β―_(γ)_/Β― μ λ λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νμ§ μκΈ° λλ¬Έμ κ°μΈμ μΌλ‘ μ΄ λ³κ²½μ λν΄ λ³λ‘ μ κ²½ μ°μ§ μμ΅λλ€. μν₯μ λ°μ§ μκΈ° λλ¬Έμ λλ€. νμ§λ§ λ§μ μ¬λλ€μ΄ μ΄ "κΈ°λ₯"μ μ κ²½μ μ°λ κ² κ°μ΅λλ€. κ·Έκ²μ΄ μ°λ¦¬κ° μ²μμ κ·Έκ²μ μΆκ°ν μ΄μ μ λλ€. μ¬κΈ°μμ μ°μ μμκ° λμ κ²μ IMHOμ λλ€.
withStyles
μ λ³κ²½ μ¬νμ λ§€μ° λ§μ‘±νμ§λ§ ramda
λλ recompose
μ κ°μ λ κΈ°λ₯μ μΈ λ€λ₯Έ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ΄ν΄λ³΄λ©΄ νμ΄νμ΄ κ·Έλ κ² μ격νμ§ μμ΅λλ€. λ λ€ μ ν μμ νμ§ μμ΅λλ€. μ’
μ’
ν¨μμ λ°ν κ°μ λνλ΄λ μ λ€λ¦μ μ λ¬ν΄μΌ ν©λλ€. μμμ§λ μμ§λ§ 99.9%μ μ¬μ©μμκ² μ ν©ν©λλ€.
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νλ μ¬λλ€μκ² ν¨κ³Όκ° μμλ μ€λλ νμ΄νμ λ€μ κ°μ Έμ¬ μ μλ μ΄μ λ 무μμ λκΉ? λ κ°μ μ λ€λ¦μ΄ μκΈ° λλ¬Έμ λ μ λ ₯μ λλν κ°μ§ μ μμ΅λλ€.
λν λ°μ½λ μ΄ν°μ κ΄λ ¨λ μ¬λλ€μ λ¬Έμ κ° λ¬΄μμΈμ§ μ½κ° νΌλμ€λ½μ΅λλ€("μλνμ§ μμ" κ΄λ ¨). Angular λ° https://nestjs.com/ κ³Ό κ°μ λΌμ΄λΈλ¬λ¦¬λ κ²°κ΅ μ΄λ₯Ό λ§μ΄ μ¬μ©ν©λλ€. μ°λ¦¬μ κ²½μ° μ¬λλ€μ μνμ WithStyles
λ₯Ό μΆκ°νλ©΄ λ©λλ€.
@sebald
λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νλ μ¬λλ€μκ² ν¨κ³Όκ° μμλ μ€λλ νμ΄νμ λ€μ κ°μ Έμ¬ μ μλ μ΄μ λ 무μμ λκΉ? λ κ°μ μ λ€λ¦μ΄ μκΈ° λλ¬Έμ λ μ λ ₯μ λλν κ°μ§ μ μμ΅λλ€.
λ¬΄μ¨ λ§μμ΄μ μ§ μ λͺ¨λ₯΄κ² μ΅λλ€. μ¬λλ€μ΄ λ°μ½λ μ΄ν°λ₯Ό κ³ ν΅ μμ΄ μ¬μ©ν μ μλ νμ΄νμ μμμ΅λλ€. 첫 λ²μ§Έ ꡬνμ λλ―Έ classes
μν(μ: <StyledComp classes={{} as any} />
μ μ λ¬νμ§ μκ³ μ₯μλ κ΅¬μ± μμμ μμλ₯Ό ꡬμ±ν μ μλ€λ #8267λ‘ μΈν΄ μ΄λ €μμ κ²ͺμμ΅λλ€. λ λ²μ§Έ(νμ¬) ꡬνμ classes
κ° κ΅¬μ± μμ ν΄λμ€ λ΄μμ μ μ¬μ μΌλ‘ undefined
λ‘ κ°μ£Όλλ λ°λμ λ¬Έμ λ‘ μ΄λ €μμ κ²ͺμ΅λλ€. νμ¬ νμμΌλ‘ TypeScript λ°μ½λ μ΄ν°λ₯Ό μ¬μ©νλ €λ©΄ μ΄ λ κ°μ§ μ΅μ
λ§ μμΌλ©΄ λ©λλ€. λΉμ μ λ
μ μ ννμμμ€.
μΈ λ²μ§Έ λ°©λ²μ μ¬λ°λ₯Έ μ νμ μ¬μ©νμ¬ classes
κ° ν΄λμ€ λ΄μμ μ μλμ§λ§ μνμΌλ‘ μ λ¬ν νμκ° μλλ‘ νλ κ²μ
λλ€. μ΄ λ κ°μ§ 쑰건μ λͺ¨λ κ°μΆλ €λ©΄ λ°μ½λ μ΄ν°λ₯Ό ν¬κΈ°ν΄μΌ ν©λλ€.
@pelotom λ€, μ£μ‘ν©λλ€. λ€κ° μ³μ. μ§μ§ λ΄λ μ μλλ°... π€ κ·ΈλΌ λ³ν©νμ!
@sebald
λν λ°μ½λ μ΄ν°μ κ΄λ ¨λ μ¬λλ€μ λ¬Έμ κ° λ¬΄μμΈμ§ μ½κ° νΌλμ€λ½μ΅λλ€("μλνμ§ μμ" κ΄λ ¨). Angular λ° https://nestjs.com/ κ³Ό κ°μ λΌμ΄λΈλ¬λ¦¬λ κ²°κ΅ μ΄λ₯Ό λ§μ΄ μ¬μ©ν©λλ€.
Angularμμ μ΄λ»κ² μ¬μ©λλμ§ μ λͺ¨λ₯΄κ² μ§λ§ λ°μ½λ μ΄ν°λ₯Ό μ½κ² μ¬μ©ν μ μλ μ¬μ© μ¬λ‘λ λΆλͺ
ν μμ΅λλ€. κΈ°λ³Έμ μΌλ‘ λ°μ½λ μ΄ν°κ° κΎΈλ―Έκ³ μλ ν΄λμ€μ μ νμ λ³κ²½ν νμκ° μλ€λ©΄ μ λλ‘ μλν©λλ€. μ°λ¦¬κ° μ²ν μν©μ κ·Έλ μ§ μμ΅λλ€. withStyles
λ μ₯μνλ κ΅¬μ± μμμ μ νμ λ³κ²½ν΄μΌ ν©λλ€.
@pelotom λ€, λ§μ΅λλ€. λμ κ²μ λμ°λ³μ΄ μΌλΏμ λλ€. μ€μ λ‘, TSκ° νμ¬ λ°μ½λ μ΄ν°λ₯Ό ꡬννλ λ°©μμ Angular μ¬μ©μμκ²λ μ’μ μ μμ΅λλ€. λ°μ½λ μ΄ν° AFAIKλ "μ΄ ν΄λμ€λ₯Ό κ΅¬μ± μμλ‘ λ±λ‘" λλ "DIμμ μ¬μ©ν μ μλλ‘ λ©ν λ°μ΄ν° μΆκ°"μ κ°μ΄ νλ μμν¬μ κ³μ½μ μ€μ νκΈ° λλ¬Έμ λλ€. " ... μ μ΄ λμ μ°κΈ°λ§ ν΄λ κΈ°λΆμ΄ μ’μμ§λ€μ π€’
@pelotom λ΄κ°νλ λ°©μμΌλ‘ μ νμ μ¬μ©νλ μ΄μ λ λ΄ κ΅¬μ± μμμ μ ν μμ μ±μ μ 곡νκΈ° λλ¬Έμ
λλ€. νμ¬ μ νμλ κ΅¬μ± μμμ κ΄λ ¨νμ¬ μ ν μμ μ΄ μμ΅λλ€. λ΄ μμ μ injectionProps λ κ΅¬μ± μμκ° μλνλ λ° νμν μ νμ
λλ€. react-redux
μ°κ²° μ νμ mapStateToProps μμ μ€λ Props λ° mapDispatchToProps μμ μ€λ DispatchProps μ μ νμ΄μ΄μΌ ν©λλ€.
맨 λμμ λ΄ λΆλͺ¨ κ΅¬μ± μμκ° λ΄κ° μ½μ ν΄μΌ νκ±°λ λ΄ νλ‘μ νΈκ° μ»΄νμΌλμ§ μλλ€λ κ²μ μ μ μλλ‘ injectionProps κ° μμ΄μΌ ν©λλ€. (μ΄κ²μ΄ λ΄κ° μνλκ±°μΌ). μ΄ λ³κ²½ μ¬νμ΄ μ΄λ¬ν μ’ λ₯μ typeSafetyλ‘ λ€μ λ§μ΄κ·Έλ μ΄μ λ©λκΉ?
@marcusjwhelan λ€μ, λ² ν 10μμ μ»΄νμΌλ μ 체(κ·Έλ¬λ μ΅μνμ) μμ λ₯Ό μ 곡ν μ μλ€λ©΄ λ§μ΄κ·Έλ μ΄μ λ°©λ²μ 보μ¬λ릴 μ μμ΅λλ€. AFAIK μ λ²μ μ μ΄μ λ³΄λ€ λͺ¨λ λΆλΆμμ ννλ ₯μ΄ νλΆνκ³ μ νμ΄ μμ ν΄μΌ ν©λλ€.
@pelotom μ΄λ¦¬μμ μ§λ¬Έ, μ 릴리μ€κ° μλ£λλ©΄ μλ¦Όμ λ°μ μ μλ λ°©λ²μ΄ μμ΅λκΉ?
@wcandillon νΈμν°μμ νλ‘μ°νμΈμ.
@pelotom μμ μμλ₯Ό
λλ κ·Έκ²μ μμ.
μ΄ ν λ‘ μ μ‘°κΈ λ¦κ² μμνμ§λ§ μ¬κΈ°μμ μΈκΈν κ²κ³Ό μ μ¬ν μ€λ₯κ° λ°μνμΌλ©° μ΄λ₯Ό μμ ν μ μλ λ°©λ²μ μμ§ κ²°μ νμ§ λͺ»νμ΅λλ€. [email protected]
λ° [email protected]
μμ΅λλ€.
const LoginForm = withStyles(styles)(
class extends React.Component<WithStyles<'root' | 'button'>, ILoginFormState> {
// ...
}
);
render(
<MuiThemeProvider theme={theme}>
<LoginForm />
</MuiThemeProvider>,
document.getElementById('login-form')
);
μ΄ νΉλ³ν κ²½μ°μ λνλλ μ€λ₯ λ©μμ§λ λ€μκ³Ό κ°μ΅λλ€.
Type '{}' is not assignable to type 'IntrinsicAttributes & WithStyles<"root" | "button"> & StyledComponentP...'.
Type '{}' is not assignable to type 'WithStyles<"root" | "button" | "progress" | "textField">'.
Property 'classes' is missing in type '{}'.
not assignable to type 'WithStyles<"root" | "button" | "progress" | "textField">'.
Property 'classes' is missing in type '{}'.
κ΅¬μ± μμ ν΄λμ€λ₯Ό μ₯μν λ WithStyles λμ StyledComponentPropsλ₯Ό μ¬μ©ν΄μΌ ν©λλ€.
@pelotom - μ΄λκ°μ μ΄κ²μ μκ° μμ΅λκΉ? TypeScriptμμ μν μ μ₯ κ΅¬μ± μμ ν΄λμ€μ μ€νμΌμ μ§μ νλ λ°©λ²μ κ²°μ νλ λ° λ§μ λ¬Έμ κ° μμ΅λλ€.
@iamjem λΉμ μ΄ μ 곡λμ§ μμκΈ° λλ¬Έμ λ§νλ 건 μ’ μ΄λ ΅λ€ styles
,νμ§λ§ 첫 λ²μ§Έ λ¬Έμ λ λΉμ μ΄ μΈκΈ ν λ ν΄λμ€ ν€λ₯Ό κ°μ§κ³ μλ€ κ² κ°μ΅λλ€ styles
λΉμ μ΄ μΈκΈνλ κ²λ³΄λ€ WithStyles<...>
. λ‘ λ°κΎΈμλ©΄ λ κ² κ°μμ
const LoginForm = withStyles(styles)(
class extends React.Component<WithStyles<keyof typeof styles>, ILoginFormState> {
// ...
}
);
첫 λ²μ§Έ λ¬Έμ λ₯Ό μ²λ¦¬ν΄μΌ ν©λλ€. κ·Έλ° λ€μ λ λ²μ§Έ λ¬Έμ κ° μλ κ² κ°μ΅λλ€. LoginForm
μ κ²°κ³Ό μ νμ λ€μκ³Ό κ°μ΅λλ€.
React.ComponentType<WithStyles<"root" | "button"> & StyledComponentProps<"root" | "button">>
μ΄κ²μ λΆλͺ
ν μ³μ§ μμ΅λλ€. non-style propsμ λΆμ¬κ° νμ
μμ€ν
μ νΌλμ€λ½κ² νλ κ²μ²λΌ 보μ
λλ€. {}
λ₯Ό μ ν μΈμλ‘ μ λ¬νμ¬ μ€νμΌμ΄ μλ propsκ° λ¬΄μμΈμ§ λͺ
μμ μΌλ‘ μ€λͺ
νλ©΄ λμμ΄ λ μ μμ΅λλ€.
const LoginForm = withStyles(styles)<{}>( // <-- note the type argument
class extends React.Component<WithStyles<keyof typeof styles>, ILoginFormState> {
// ...
}
);
λ무 볡μ‘ν΄μ μ£μ‘ν©λλ€. μ΄ μμ μ λ μ½κ² μνν μ μλ λ°©λ²μ μκ³ μΆμ΅λλ€!
κ·Έ νΈλ¦μ νλ€! λΉ λ₯Έ λμμ μ£Όμ μ κ°μ¬ν©λλ€ @pelotom. λλ μ€λ«λμ Reactλ₯Ό μ¬μ©ν΄ μμ§λ§ μ΅κ·Όμ Material-uiμ λ°μ΄λ€μκ³ , μ¬μ©νλ λμ Typescriptλ₯Ό μ¬μ©ν΄ λ³΄κ³ μΆλ€κ³ μκ°νμ΅λλ€. λ§ν νμλ μμ΄ Typescriptλ₯Ό ν볡νκ² λ§λλ λ°©λ²μ λ§νκΈ° μ΄λ €μ΄ λͺ κ°μ§ κ·Ήλ¨μ μΈ κ²½μ°κ° μμ΅λλ€.
λλ μ¬κ΅¬μ±μ μ¬μ©νμ¬ ν΄κ²°νλ€.
μμ
import { StyleRules, Theme, withStyles } from "material-ui/styles";
import * as React from "react";
import { compose } from "recompose";
interface IProps {
classes?: any; // <-- add question mark
text: string;
}
const styles = (theme: Theme): StyleRules => ({
root: {
},
});
@compose(
withStyles(styles),
)
export class MyComponent extends React.Component<IProps> {
render() {
const { classes, text } = this.props;
return (
<div className={classes.root}>
{text}
</div>
);
}
}
λλ μ§λ λ©°μΉ λμ λͺ νν κ²°κ³Ό μμ΄ μ΄ λ¬Έμ (λ° #8704)λ‘ μΈμ°κ³ μμμ΅λλ€. κ·Έλ° λ€μ @pelotom μ μ‘°μΈμ
λΉμ μ μ¬μ©ν΄μΌ
StyledComponentProps
λμWithStyles
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ μ μ¬ν λ°©λ²μ GitHubμμ κ²μνμ΅λλ€. κ·Έλ¦¬κ³ νλμ μλ μλ₯Ό μ°Ύμμ΅λλ€ π. κ·Έλλ μ’μ λ°©λ²μ΄κ³ λ΄ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€. TypeScriptκ° ν볡ν μΏ ν€μΈ λ³λμ 컨ν μ΄λμ κ΅¬μ± μμ: μ¬κΈ°μ μΈκΈλ μ ( μ°Έκ³ : μ κ²½μ° κ΅¬μ± μμ 맀νμ λ³λμ 컨ν μ΄λ νμΌμ μμ§λ§ μμ΄λμ΄λ κ°μ.).
λκ΅°κ° μ΄κ²μ΄ λμ ν΄κ²°μ± μ΄λΌκ³ μκ°νλ€λ©΄ λλ μ΄λ€ λ³νμ μμ΄λμ΄μλ μ΄λ € μμ΅λλ€. μ§κΈμ λ΄ μ½λκ° λΆνμ λ©μΆ κ² κ°μ κΈ°μ©λλ€.
type Styles = 'foo';
const styles: StyleRulesCallback<Styles> = (theme: Theme) => ({
foo: {
position: 'relative',
}
});
interface Props {
something: string;
}
class Sidebar extends React.Component<Props & WithStyles<Styles>> {
render() {
const { classes, something } = this.props;
return (
<div className={classes.foo}>{something}</div>
);
}
}
export default withStyles(styles)<Props>(Sidebar);
μ λ¬Έμ λ₯Ό λ§λ€κ³ μΆμ§λ μμ§λ§ λ¬Έμ, μμμ λ³Έ λͺ¨λ κ²μ μλνκ³ μ¬κ΅¬μ±μ μ¬μ©νμ¬ λ¬Έμ λ₯Ό μ λ¬νμ§λ§ μΌλΆ μμ±μ μΆκ°νλ©΄ κ΅¬μ± μμκ° μλνλλ‘ λ§λ€ μ μμ΅λλ€.
κ·Έλ¦¬κ³ λ΄κ° μ°Ύμ 리μμ€λ λλΆλΆ μ΄μ λ²μ μ TS, MUI λλ Reactμ ν¨κ» μμ΅λλ€.
λ΄ κ΅¬μ± μμλ λ€μκ³Ό κ°μ΅λλ€.
import React from 'react';
import AppBar from 'material-ui/AppBar';
import { withStyles, WithStyles, StyleRulesCallback } from 'material-ui/styles';
const styles: StyleRulesCallback<'positionFixed'> = () => ({
positionFixed: {
top: 'auto',
bottom: 0,
},
});
const decorate = withStyles(styles);
interface Props {
someProp: string;
};
export const BottomNav = decorate<Props>(
class extends React.Component<Props & WithStyles<'positionFixed'>, {}> {
render() {
return (
<AppBar />
);
}
}
);
export default BottomNav;
κ·Έλ¦¬κ³ μ€λ₯λ λ€μκ³Ό κ°μ΅λλ€.
TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & Props & StyledComponentProps<"positionFixed"> & { children?: ReactNode; }'.
Type '{}' is not assignable to type 'Props'.
Property 'someProp' is missing in type '{}'.
μ λ TSμ μ΄λ³΄μμ΄μ§λ§ μ€λͺ μ νμ΄μ§ μ μμ κ° λ§€μ° νΌλμ€λ½κ±°λ μμ νμ§ μλ€λ κ²μ μμμ΅λλ€.
νΉμ μμλλΆ κ³μλ©΄ μλ €μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€ ;-)
@otroboe μ€μ λ‘ μ€λ₯ νλκ·Έκ° μ§μ λ μ½λλ₯Ό μλ΅νκ³ μμ΅λκΉ? μ΄λ κ² μκ²Όλμ?
<BottomNav />
κ·Έλ λ€λ©΄ λ¬Έμ λ someProp
μν( Props
μ μμ λ°λΌ νμ)μ μ 곡ν΄μΌ νλ€λ κ²μ
λλ€.
<BottomNav someProp="foo" />
λΆλλ½λ€... μ€, λΆλλ½λ€.
λλ TS ν΅ν©μ λ무 μ§μ€ν΄μ μ£Όλ³μ λλ¬λ³΄κ³ λͺ κ±Έμ λ€λ‘ λ¬Όλ¬λλ κ²μ μμμ΅λλ€.
κ°μ¬ν©λλ€ @pelotom :μ€λ§μΌ:
@otroboe λ¬Έμμ΄ μ€λ³΅λ μ κ±°...
type Styles = 'positionFixed';
κ·Έλλ μ’ λ μ¬μ μΌλ©΄...
λ€ μ λ κ·Έλ¬μ΄μ, κ°μ¬ν©λλ€ :+1:
λ°©κΈμ΄ λμΌν λ¬Έμ μ μ§λ©΄νμ§λ§ ν΄λμ€ λλ ν¨μμ λμΌν νμΌμμ μ€νμΌ κ°μ²΄λ₯Ό μ΄κΈ°νν κ²½μ°μλ§ λ°μνλ κ²μΌλ‘ λνλ¬μ΅λλ€. λλ λ€λ₯Έ νμΌμμ μ€νμΌμ κ°μ Έμ€λ κ²½μ° μ΄ μ€λ₯κ° λ°μνμ§ μμ΅λλ€.
μ μ΄λ° μΌμ΄ λ°μνλμ§ λ¨μκ° μμ΅λκΉ?
@nishmeht7 μ체 ν¬ν¨λ μ€λν«μ κ²μν μ μμ΅λκΉ?
@pelotom λ°©κΈ νλ λ§λλ μμ μ νμΌλ©° λ΄ μλλ°μ€ νκ²½μμ μ μλν©λλ€. μ λ νμ¬ ν° μ±μμ μμ μ€μ΄λ©° μ¬μ ν mui λ²μ 1.2.2λ₯Ό μ¬μ©νκ³ μμΌλ©° λ΄ μλλ°μ€ νκ²½ λ²μ μ μ΅μ λ²μ μ λλ€. λ°λΌμ λ²μ μ μ κ·Έλ μ΄λνλ©΄ μ€λ₯λ₯Ό μ¬νν μ μμ κ²μ΄λΌκ³ μΆμΈ‘νκ³ μμ΅λλ€.
λλ https://material-ui.com/demos/selects/ μ κΈ°λ³Έ μμμ μλ₯Ό λ°λμ§λ§ theme
root
κ° μλ€λ λΆλ§μ λ°μμ΅λλ€(μ΅μ νμ΄νμ€ν¬λ¦½νΈ λ° material- ui), form
μμμ ν΄λμ€κ° μ μ©λμ§ μμμ΅λλ€. μμ λ
Όμλ₯Ό λ°λΌ 보μμ§λ§ κ²°μ μ μ΄μ§ μμ κ² κ°μ΅λλ€. κ·Έλ¦¬κ³ μ€μ λ‘ μμλ ν΄λμ€ λͺ©λ‘μλ form
λν΄ μμ±λ ν΄λμ€ μ΄λ¦μ΄ λλ½λμμ΅λλ€. μμ±λ ν΄λμ€ μ΄λ¦μ μλμΌλ‘ μΆκ°ν κ²½μ°(κ°λ° λꡬμμ console.log(theme)
λ₯Ό μ¬μ©νμ¬ withStylesμμ μ°Ύμ μ μμ΅λλ€. λͺ¨λ κ²μ΄ μ μμ΄λ―λ‘ λΆλͺ
ν ν΄λμ€κ° μ¬λ°λ₯΄κ² μμ±λκ³ μμμ΅λλ€. withStyles
λ₯Ό ν΅ν΄ μ λ¬λμ§ μμμ΅λλ€. form
μμμ
λλ€. κ½€ νΌλμ€λ½μ΅λλ€.
κ·Έλμ μ λ μ΄κ²μ΄ μ λ ¬λ λκΉμ§ μ§κΈμ μ€νμΌλ‘ λλμκ°μ΅λλ€.
<form style = {{display:'flex',flexWrap:'wrap'}} autoComplete="off">
@HenrikBechmann typescriptμ λν μ€νμΌ λ¬Έμλ₯Ό λ°λΌν΄ λ³΄μ ¨μ΅λκΉ? κ³Όκ±°μλ μ μκ² κ½€ λμμ΄ λμμ΅λλ€. https://material-ui.com/guides/typescript/
@lookfirst κ°μ¬ν©λλ€! λλ κ·Έ λ¬Έμλ₯Όλ³΄κ³ (μ²μμ μλμ§λ§ :-)) μ¬μ©νμ΅λλ€.
export default withStyles({
root: {
display: 'flex',
flexWrap: 'wrap',
},
})(BaseForm)
(ν¨μκ° μλ κ°μ²΄ μ λ¬)
... νμ΄νμ€ν¬λ¦½νΈ μ€λ₯λ₯Ό ββνΌνκ³ μμ±λ ν΄λμ€μ μ½μ μ νμ±ννμ΅λλ€.
λ€λ₯Έ νμ΄ λ 볡μ‘ν ꡬμ±μ λμμ΄ λκΈ°λ₯Ό λ°λλλ€.
λν styles
ν¨μμ λ 볡μ‘ν κ΅¬μ‘°κ° μλν¨μ νμΈνμ΅λλ€(μμ±λ className
μ form
μ£Όμ
).
import React from 'react'
import { withStyles, createStyles, Theme } from '@material-ui/core/styles'
/*
patterned after first demo https://material-ui.com/demos/selects/ for 3.03
use Typsecript fixes from here: https://material-ui.com/guides/typescript/
*/
const styles = (theme:Theme) => createStyles({
root: {
display: 'flex',
flexWrap: 'wrap',
},
})
class BaseForm extends React.Component<any,any> {
render() {
const { classes } = this.props
return (
<form className = {classes.root} autoComplete="off">
{this.props.children}
</form>
)
}
}
export default withStyles(styles)(BaseForm)
νΈμ§: @eps1lon μ WithStyles
μ¬μ© μ μ΄κ²μ΄ λΆνμνλ€κ³ μ§μ νμ΅λλ€!
λ΄κ° μ¬μ©νλ μΌλΆ μ±κ³΅ νμ΄ ReturnType<T>
μμ±νκΈ° ClassKey
λλ₯Ό μν΄λ₯Ό :
import * as React from 'react';
import withStyles, {
StyledComponentProps,
StyleRulesCallback,
} from '@material-ui/core/styles/withStyles';
import { Theme } from '@material-ui/core/styles/createMuiTheme';
const overrideStyles = (theme: Theme) => ({
root: {
display: 'flex',
flexDirection: 'column',
},
});
type Props = StyledComponentProps<keyof ReturnType<typeof overrideStyles>>;
class MyComponent extends React.PureComponent<Props> {
render() {
return <div className={this.props.classes.root}></div>;
}
}
export default withStyles(overrideStyles as StyleRulesCallback, {withTheme: true})(MyComponent);
keyof ReturnType<typeof styleOverrides>
λ₯Ό StyledComponentProps
ClassKey
νλ©΄ overrideStyles
λ°νλ κ°μ²΄μμ ν€λ₯Ό κ°μ Έμ€κ³ ν΄λΉ ν€ λͺ©λ‘μ μλμΌλ‘ μ μ§νμ§ μμλ λ©λλ€. . λ΄κ° μμμ°¨λ¦° μ μΌν κ²°ν¨μ withStyles
νΈμΆμμ overrideStyles as StyleRulesCallback
λ₯Ό μΊμ€ν
νμ§ μμΌλ©΄ μ ν μ€λ₯μ
λλ€. μ΄μ λ₯Ό 100% νμ ν μ μμ΅λλ€. μ΄λ€ μ΄μ μμμΈμ§ overrideStyles
κ° λ¬΄μμΈμ§ μ΄ν΄νμ§ λͺ»νλ withStyles
κ² κ°μ΅λλ€.
λ€μ 볡μ‘ν μ νμ λͺ
νν νκΈ° μν΄ typeof styleOverrides
λ μ€νμΌ κ°μ²΄λ₯Ό λ°ννλ ν¨μλ‘ ν΄μλ©λλ€. ReturnType<T>
λ μ€νμΌ κ°μ²΄ μ체λ₯Ό κ°μ Έμ΅λλ€. keyof
λ μ€νμΌ κ°μ²΄μμ ν€λ₯Ό κ°μ Έμ΅λλ€.
@chrislambe typescript κ°μ΄λλ₯Ό νμΈν΄μΌ ν©λλ€. ReturnType
λ±μ μ¬μ©ν νμλ μμ΅λλ€. createStyles
λ° WithStyles
λ λμ°λ―Έλ‘ μΆ©λΆν΄μΌ ν©λλ€.
@eps1lon μ€, μ λ§
fwiw λλ createStyles/withStyles μμ μ¬μ©ν¨μ λ°λΌ μ μ λ μ’μν©λλ€. κΉλν μ½λλ₯Ό ν보νκ³ λͺ¨λ μ€νμΌλ§/νμ μ€ν¬λ¦½νΈ λ¬Έμ λ₯Ό μ²λ¦¬νλ©° λ‘컬 μ‘°κ±΄λΆ CSSλ₯Ό μνλ©΄ ν΄λμ€λ³΄λ€ μ°μ νλ λ‘컬 μ€νμΌ μμ±μ μμ±ν©λλ€.
λ©μ§!!
@material-ui/ core @ Test does not have required attribute classes
λ₯Ό μ»μ΅λλ€.
import React from 'react'
import { Theme, WithStyles, withStyles, createStyles } from '@material-ui/core/styles'
const styles = (theme: Theme) => createStyles({
root: {
color: theme.palette.action.active
},
})
interface Props extends WithStyles<typeof styles> {
asd: boolean
}
class TestComponent extends React.Component<Props> {
render() {
const { classes } = this.props
return (
<div className={classes.root}>
</div>
)
}
}
const Test = withStyles(styles)(TestComponent)
const a = () => <Test asd={true}/>
@aaronlifton2
WithStylesλ₯Ό μ¬μ©νμ¬ μν κ°νλ₯Ό λ°λ₯΄μμμ€.
@valoricDe μ μ€ λ¬Έμ λ ν΄κ²°νμ ¨λμ?
@TrejGun λ°©κΈ νμΈνμ΅λλ€. κΈ°λ₯ κ΅¬μ± μμμ @material-ui/ core @
λ μ λ§ μ΄ν΄κ° μ λΌμ.
λ€μ λ¬Έμ: https://material-ui.com/guides/typescript/#augmenting -your-props-using-withstyles
μ΄ μλ λ¬Έμ μ λ¬Έμ λ₯Ό μΌμΌν€λ κ² κ°μ΅λλ€. μ΄λμμ κ΅¬μ± μμλ₯Ό μ¬μ©ν λ typescriptλ ν΄λμ€ μμ±μ΄ withStylesμ μν΄ _μ£Όμ
_λ κ²μ΄λΌλ κ²μ κΉ¨λ«μ§ μκ³ μνμΌλ‘ _μ λ¬_νλλ‘ μꡬν©λλ€.
λλ μ§κΈ λͺ μκ° λμ μ΄λ¬ν λ¬Έμ λ₯Ό μ½μμ§λ§ μ λ§ μ΄ν΄κ° λμ§ μμ΅λλ€. μ΄ μμ μμ μ΄λ€ λμμ΄λΌλ λ무 μ’μ κ²μ λλ€.
μμ μ μμ λν΄
@cfilipov λΉμ μ΄ μ¬μ©νλ κ΅¬μ± μμ ν΄λμ€λ₯Όνλ€ μ₯μ ν λ
StyledComponentProps
λμWithStyles
, κ·Έλ¦¬κ³ μ격ν νμ κ²μ¬ λͺ¨λμμ λΉμ μ nullμ΄ μλ μ£Όμ₯ μ°μ°μλ₯Ό μ¬μ©ν΄μΌ ν κ²½μ°!
μΆμΆλ¬Όμμclasses
μ νλ μ΄κ²μwithStyles
λ₯Ό ν΄λμ€ λ°μ½λ μ΄ν°λ‘ μ¬μ©ν μ μλλ‘ νλ μ μΆ©μμ λλ€. TypeScriptμ ν΄λμ€ λ°μ½λ μ΄ν°λ λ°ν μ νμ΄ μΈμ μ νκ³Ό μΌμΉν΄μΌ νλ€λ μ ν μ΄ μμ΅λλ€. μ΄ μ ν μ¬νμ΄ μλ κ²½μ° ν κ°μ§ μ΅μ λ§ κ°λ₯ν©λλ€.
withStyles
λ₯Ό ν΄λμ€ λ°μ½λ μ΄ν°λ‘ μ¬μ©νλ κ²μ μ§μνμ§ μμ΅λλ€.- μ₯μλ κ΅¬μ± μμ μ νμ μμλ₯Ό ꡬμ±ν λ λλ―Έ
classes
μνμ μ λ¬νλλ‘ μꡬν©λλ€(μ΄μ μ μΆ©μμ νλ¦Όμμ΄ λ μ§μ¦λ¬μ )classes
κ° κ΅¬μ± μμ λ΄μμ nullableλ‘ κ°μ£Όλλλ‘ μꡬνμ¬ ν΄λΉ νλμ μ‘μΈμ€ν λ!
μ°μ°μλ₯Ό μ¬μ©ν΄μΌ ν©λλ€( νμ¬ νν ).κ΅¬μ± μμμ μνκ° μλ κ²½μ° μν λΉμ μ₯ κΈ°λ₯ κ΅¬μ± μμλ₯Ό μ¬μ©νλ κ²μ΄ μ’μ΅λλ€. κ·Έλ¬λ©΄ μ ν μ£Όμμ΄ λ μ κ² νμνκ³ μ νμ΄ λ μμ ν©λλ€.
export default withStyles(styles)<Props>(({ classes, message }) => ( <div className={classes.main}> <div className={classes.foo}> Hello World! {message} </div> </div> ));
StyledComponentProps
μ¬μ© λ°©λ²μ μ΄λ»κ² μ μ μμ΅λκΉ? μ€νμΌ κ°μ²΄μ μ μλ ν€μ λ¬Έμμ΄μ μ λ¬νλ κ² κ°μ΅λλ€.
κ·Έλ¬λ λ¬Έμλ λ¨μν μλνμ§ μλ μΌμ νλΌκ³ μ°λ¦¬μκ² λ§ν©λκΉ? λ΄κ° 무μμ λμΉκ³ μμ΅λκΉ? https://material-ui.com/guides/typescript/#augmenting -your-props-using-withstyles...λ₯Ό μ¬μ©νκ³ μΆμ΅λλ€.
μ΄κ² κ°λ₯ν΄?
@valoricDe , ν΄λΉ λ¬Έμ κ° μλ κΈ°λ₯ κ΅¬μ± μμλ₯Ό μ΄λ»κ² μννμ΅λκΉ?
@TrejGun λ°©κΈ νμΈνμ΅λλ€. κΈ°λ₯ κ΅¬μ± μμμ @material-ui/ core @
λλ λ€μκ³Ό κ°μ κ²μ μλνκ³ μμ΅λλ€.
```import React, { ChangeEvent, Component, Dispatch } from "react";
"prop-types"μμ PropTypeμ κ°μ Έμ΅λλ€.
"react-redux"μμ { μ°κ²° } κ°μ Έμ€κΈ°;
"@material-ui/core"μμ { 그리λ, FormControlLabel, ν
λ§, createStyles, withStyles, Radio, WithStyles } κ°μ Έμ€κΈ°;
"./Amount"μμ κΈμ‘ κ°μ Έμ€κΈ°;
"../../store/actions/selectPaymentAmountActions"μμ { onPastDueFormFieldChange } κ°μ Έμ€κΈ°;
const μ€νμΌ = (ν
λ§: ν
λ§) =>
createStyles({
μ: {
alignSelf: "μ€μ",
},
});
μΈν°νμ΄μ€ OwnPropsλ WithStylesλ₯Ό νμ₯ν©λλ€.
μΈν°νμ΄μ€ StateProps {
κΈ°νμ΄ μ§λ¬μ΅λκΉ?: λ²νΈ;
pastDueOrTotalOrOther: λ¬Έμμ΄;
}
μΈν°νμ΄μ€ DispatchProps {
onPastDueFormFieldChange: OnPastDueFormFieldChange;
}
μ ν μν = StateProps & DispatchProps & OwnProps;
const PastDueFormField = withStyles(μ€νμΌ)(
({ ν΄λμ€, pastDue, pastDueOrTotalOrOther, onPastDueFormFieldChange }: μν) => (
checked={κ³Όκ±° λ§κ°μΌ λλ μ 체 λλ κΈ°ν === "κ³Όκ±° λ§κ°μΌ"}
onChange={onPastDueFormFieldChange}
label="κΈ°ν κ²½κ³Ό:"
컨νΈλ‘€={
/>
),
);
const mapStateToProps = (μν: RootState): StateProps => ({
κ³Όκ±° κΈ°ν: state.customerData.balanceDue.pastDue,
pastDueOrTotalOrOther: state.customerPaymentsForm.pastDueTotalOrOther,
});
κΈ°λ³Έ μ°κ²° λ΄λ³΄λ΄κΈ°
mapStateToProps,
{ onPastDueFormFieldChange },
)(κ³Όκ±° λ§κ° μμ νλ);
When I use this component I have this error:
```import PastDueFormField
Property 'classes' is missing in type '{}' but required in type 'Readonly<PropsWithChildren<Pick<Pick<Props, "pastDue" | "pastDueOrTotalOrOther" | "onPastDueFormFieldChange"> & StyledComponentProps<"amount">, "classes" | "innerRef"> & OwnProps>>'
@yehudamakarov λ¨Όμ react-redux
μμ΄ μ½λλ₯Ό μμ±νκ³ λͺ¨λ κ²μ΄ μμλλ‘ μλνλ©΄ connect
μΆκ°νμμμ€. propμ΄ μ£Όμ
λ λ μ’μ κ°μλ₯Ό μ»λ κ²μ λ―Ώμ μ μμ μ λλ‘ μ΄λ ΅μ΅λλ€.
μ΄λ¬ν λ¬Έμ κ° λ°μνλ©΄
missing props
λ κΉ¨λν μ½λλ₯Ό μ΄μ§ν©λλ€. νΉν μμ
μμμ κ΄λ ¨νμ¬. νμ¬ withStyles
μ connect
λ₯Ό ꡬλΆνμ§ μκ³ νΌν©νκ³ μμ΅λλ€.
μΈλ°μ€μ°¬ μ λ§ κ°μ¬ν©λλ€.
μ°κ²°μ μΌλ° μΈμλ₯Ό μ λ¬νμ§ μμμΌλ‘μ¨ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€. <StateProps ...>
λ©μ΄λ¦¬λ₯Ό κΊΌλμ΅λλ€.
λλ κ·Έ μΌλ° μΈμκ° WithStyles<>λ₯Ό νμ₯νλ λ΄ OwnProps μΈν°νμ΄μ€λ₯Ό μλ§μΌλ‘ λ§λ€κ³ μλ€κ³ μκ°ν©λλ€.
μ΄μ¨λ κ΅¬μ± μμμ λͺ¨λ κ²μ μ λ¬νκΈ° λλ¬Έμ λ΄ Propsμμ μ»μ μ ν κ²μ¬λ‘ μΆ©λΆν©λλ€. idμ connect<> μ λ€λ¦μ΄ νμν μ΄μ λ₯Ό μ λͺ¨λ₯΄κ² μ΅λλ€.
κ°μ¬ ν΄μ!
νμ΄νμ μ¬λ°λ₯΄κ² νλ€λ©΄ ν΄λμ€λ₯Ό "μ₯μ"νλ λͺ¨μ΅μ λ€μκ³Ό κ°μ΅λλ€.
type NonStyleProps = { text: string }; const styles = { root: { backgroundColor: 'red' } }; const DecoratedComponent = withStyles(styles)( class extends React.Component<NonStyleProps & WithStyles<'root'>> { render() { return ( <div className={this.props.classes.root}> {this.props.text} </div> ); } } );
κ·Έλ¦¬κ³ λ΄λΆμ ν λ§(useTheme())λ₯Ό μΆκ°νλ λ°©λ²μ λ€μκ³Ό κ°μ΅λλ€.
const decorate = withStyles((theme: Theme) => ({
root: {
display: "flex"
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
transition: theme.transitions.create(["width", "margin"], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen
})
}
})
κ°μ₯ μ μ©ν λκΈ
λλ μ¬κ΅¬μ±μ μ¬μ©νμ¬ ν΄κ²°νλ€.
μμ