TypeScript ๋ฒ์ : 3.4.5
๊ฒ์์ด:
๋ฐ์ ๊ธฐ๋ณธ ์ํ defaultProps ๊ธฐ๋ฅ ๊ตฌ์ฑ ์์ ์ํ ๋น ์ ์ฅ
์ํธ
import React from "react";
interface Props {
name: string;
optional: string;
}
const Component = ({ name, optional = "default" }: Props) => (
<p>{name + " " + optional}</p>
);
const Test = () => <Component name="test" />;
์์๋๋ ๋์ :
์ ๋ฐ๋ฅด๋ฉด ํ์ดํ ๋ผ์ดํฐ 3.0 ๋ฆด๋ฆฌ์ค ๋
ธํธ ๋ optional
์ํ์ด ํ์ํ์ง ๋ง์์ผ Test
๊ทธ๊ฒ์ด ES2015 ๊ธฐ๋ณธ ์ด๊ธฐํ๊ฐ์์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ๊ธฐ๋ณธ์ผ๋ก ์ ์ ๋๋๋ก Component
.
์ค์ ํ๋ :
๋ค์ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
'{name : string; ์ ํ์'optional '์์ฑ์ด ์์ต๋๋ค. } '์ด์ง๋ง'Props '์ ํ์๋ ํ์ํฉ๋๋ค.
ํ๋ ์ด ๊ทธ๋ผ์ด๋ ๋งํฌ :
๋งํฌ
๋์ดํฐ๊ฐ TSX๋ฅผ ์ง์ํ๋ ๊ฒ ๊ฐ์ง ์์ต๋๋ค.
๊ด๋ จ ๋ฌธ์ :
์ด์ ๋ํ ์ค๋ช
์๊ฐ ๋ช
ํํ์ง ์์ ์ ์์ง๋ง Props
์ธํฐํ์ด์ค์์ ์์ฑ์ ์ ํ ์ฌํญ์ผ๋ก ํ์ํด์ผํฉ๋๋ค.
interface Props {
name: string;
optional?: string;
}
๋ฆด๋ฆฌ์ค ๋ ธํธ์ ๋ด์ฉ๊ณผ ์ผ์นํ์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
export interface Props {
name: string;
}
// ...
function Greet({ name = "world" }: Props) {
return <div>Hello {name.toUpperCase()}!</div>;
}
prop์ ์ ํ์ ์ผ๋ก ๋ง๋๋ ๊ฒ์ ์ปดํฌ๋ํธ ์ธ๋ถ์์ (ํ ์คํธ์์) props์ ์ ๊ทผ ํ ๋ ์ ํ ์์คํ ์ด prop์ด ํญ์ ์กด์ฌํ๋ค๊ณ ๋ณด์ฅํ์ง ์๋๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
์:
const wrapper = shallow(<Test/>);
// optional is string | undefined instead of string
const optional = wrapper
.find(Component)
.props()
.optional;
optional
๊ฐ ํจ์ ์๋ค๋ฉด ์คํํ๊ธฐ ์ ์ ์กด์ฌํ๋์ง ํ์ธํด์ผํฉ๋๋ค. ์๋ฅผ ๋ค์ด preventDefault
๋ง ํธ์ถํ๋ ๊ธฐ๋ณธ onClick
prop์ ์ ๊ณตํ๋ ๊ฒฝ์ฐ์
๋๋ค.
TypeScript 3.1 ์ด์์์๋ ๋ค์์ ์์ฑํ ์ ์์ด์ผํฉ๋๋ค.
export interface Props {
name: string;
}
function Greet(props: Props) {
return <div>Hello {props.name.toUpperCase()}!</div>;
}
Greet.defaultProps = {
name: "world",
} as Partial<Props>; // Good practice, without it you could use a number as default prop and TypeScript wouldn't complainโฆ
const component = <Greet />; // no errors
๊ธฐ๋ณธ ์ด๋์ ๋ผ์ด์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ๊ธฐ๋ณธ ์ํ์์ ์๋ํ์ง ์๋ ๋ฒ๊ทธ์ ๋๊น? ๋ฆด๋ฆฌ์ค ๋ ธํธ์ ์ ๊ณต๋ ์์ ๊ฐ ์ต์ TypeScript ๋ฒ์ ์์ ์๋ํ์ง ์๋๋ค๋ ์ ์ ๊ฐ์ํ ๋ ํ๊ท์ฒ๋ผ ๋ณด์ ๋๋ค.
@tbassetto ๋ฌธ์ ๋ TS๊ฐ ์ค์ ๋ก ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ๋๋ก ๊ฐ์ํ์ง ์๋๋ค๋ ๊ฒ์ ๋๋ค.
import React from 'react';
interface Props {
myProp: string;
}
const Comp = (props: Props) => {
return (
<div>
{props.myProp.toUpperCase()}
</div>
);
};
Comp.defaultProps = {
} as Pick<Props, 'myProp'>;
const comp = <Comp />;
๋ ๋ค๋ฅธ ์ฑ๊ฐ์ ์ ์ ์ฒ์์์ด ์์
์ ์ํํด์ผํ๋ค๋ ์ฌ์ค์
๋๋ค. Comp
๋ฅผ React.FC<Props>
๋ก ์
๋ ฅํ๋ฉด defaultProps
๋ ์ ์
๋ ฅ๋์ง๋ง ๋ค๋ฅธ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. .
๋์ฒด๋ก ํ์ฌ TypeScripts JSX / React ์ง์์ ์ด์ํ๊ฒ ์๋ฒฝํ์ง ์์ "์ํ ์ธ ๊ฒ์ฒ๋ผ ๋ณด์ด์ง๋ง ๋ชจ๋ ๊ฒ์ด ์ ์์ธ ๊ฒ์ฒ๋ผ ์คํ ์ค์ ๋๋ค (์ ์ด๋ ๋ด ๋์๋-์ฃผ๋ก ์๋ฆฌ๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. 3.4.5์ ๋ํ ์ ํ ์์ ๋ฐฉ์์ผ๋ก ์ํ ๋น ์ ์ฅ React ๊ตฌ์ฑ ์์๋ฅผ ์ํํ๋ "๊ณต์"๋ฐฉ์์ด ๋ฌด์์ธ์ง์ ๋ํ ์ฐธ๊ณ ์ฌํญ).
๋ชจ๋ ์ฌ๋๋ค์ด ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ธ๊ธํ๋ ๊ฒ์ ๋ณด์์ง๋ง ๊ทธ๋ค ์ค ์ด๋ ๊ฒ๋ ์๋ํ๊ฒ ํ ์๋ ์์ต๋๋ค.
๋ฒ์ 3.3.3์ ๋๋ค.
export interface Props {
shouldTruncateContent: boolean;
}
const Alert: React.FunctionComponent<Props> = ({
children,
shouldTruncateContent = false
}) => {
return (
<S.Content shouldTruncateContent={shouldTruncateContent} size="fill">
{children}
</S.Content>
);
};
Alert.defaultProps = {
shouldTruncateContent: false
} as Pick<Props, 'shouldTruncateContent'>;
export default Alert;
์ ์ด๊ฒ์ด ์ฌ์ ํ ์ค๋ฅ๋ฅผ ๋์ง๋ ์ง์ ๋ํ ํต์ฐฐ๋ ฅ์ด ์์ต๋๊น?
TS2741: Property 'shouldTruncateContent' is missing in type '{ children: string; }' but required in type 'Props'.
๋๋ # 27425์ # 519์์ ์ ์ ๋ ๋ชจ๋ ์์ ์ ์๋ํ๋ค.
@tomspeak ๋ด ํด๊ฒฐ ๋ฐฉ๋ฒ ๋๋ @Hotell ์ woraround (๋ชจ๋ ์ํ์ ์ ํ ์ฌํญ์ผ๋ก ๋ง๋ค์ง ๋ง) ์ค ํ๋์ ๋๋ค. ์ํ ์ฝ๋๋ก 3.5์์ ํ ์คํธ๋์์ต๋๋ค.
๋ชจ๋ ์ฌ๋๋ค์ด ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ธ๊ธํ๋ ๊ฒ์ ๋ณด์์ง๋ง ๊ทธ๋ค ์ค ์ด๋ ๊ฒ๋ ์๋ํ๊ฒ ํ ์๋ ์์ต๋๋ค.
๋ฒ์ 3.3.3์ ๋๋ค.
export interface Props { shouldTruncateContent: boolean; } const Alert: React.FunctionComponent<Props> = ({ children, shouldTruncateContent = false }) => { return ( <S.Content shouldTruncateContent={shouldTruncateContent} size="fill"> {children} </S.Content> ); }; Alert.defaultProps = { shouldTruncateContent: false } as Pick<Props, 'shouldTruncateContent'>; export default Alert;
์ ์ด๊ฒ์ด ์ฌ์ ํ ์ค๋ฅ๋ฅผ ๋์ง๋ ์ง์ ๋ํ ํต์ฐฐ๋ ฅ์ด ์์ต๋๊น?
TS2741: Property 'shouldTruncateContent' is missing in type '{ children: string; }' but required in type 'Props'.
๋๋ # 27425์ # 519์์ ์ ์ ๋ ๋ชจ๋ ์์ ์ ์๋ํ๋ค.
shouldTruncateContent : boolean; ์ ํ์ shouldTruncateContent?: boolean;
์ด์ ๋ํ ์ค๋ช ์๊ฐ ๋ช ํํ์ง ์์ ์ ์์ง๋ง
Props
์ธํฐํ์ด์ค์์ ์์ฑ์ ์ ํ ์ฌํญ์ผ๋ก ํ์ํด์ผํฉ๋๋ค.interface Props { name: string; optional?: string; }
์ด ์ต์ ์ ํ์ ๋ชจ๋ ๊ณณ์์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ์ ๋ฐ๋ํ? ๋๊ตฐ๊ฐ ์ค๋ช ํ ์ ์์ต๋๊น?
@ralexhassle , ๋๋ ์๋ ํ๋ฅผ ์ค๋ช
ํ ์ ์์ง๋ง ์ด๊ฒ์ด ๊ตฌ์ฑ ์์์ ์ ํ์ ์์ฑ์ ์
๋ ฅํ๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์์ ์ค๋ช
ํ ์ ์์ต๋๋ค.
์ง๋ฌธ์ ์์์ ์ฌ์ฉํ์ง ์๋ defaultProps
์ฌ์ฉํ๋ ๊ฒ์ ์ ํธ ํ ์ ์์ง๋ง ๋ด๊ฐ ์๋ ํ ์ ํ์ ์์ฑ์ ์ ํ ์ฌํญ์ผ๋ก ํ์ํ๋ ๊ฒ์ ์๋ง๋ ์๋ฒฝํ๊ฒ ๊ด์ฐฎ์ต๋๋ค ...
๋ฌธ์ ์ ๋ชฉ์ ์ props๊ฐ ์ ํ ์ฌํญ์ด ์๋์ง๋ง ๊ธฐ๋ณธ๊ฐ์ด ์๋ค๋ ๊ฒ์
๋๋ค (๋ฐ๋ผ์ ๊ตฌ์ฑ ์์๋ฅผ ์ฌ์ฉํ ๋ ์ ์ ์์). ?
๋ก prop์ ์ ํ์ ์ผ๋ก ํ์ํ๋ฉด TS๋ ์ ์๋์ง ์์ ๊ฒ์ผ๋ก ๊ฐ์ฃผํ๊ณ ํด๋น ์ผ์ด์ค๋ฅผ ์ฒ๋ฆฌํ๋๋กํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ธฐ๋ณธ๊ฐ์ด ์์ผ๋ฏ๋ก ์ ์๋์ง ์์ต๋๋ค. ๋ฐ๋ ํฌํ๋ ํด์ค์๊ฐ ๋ฌธ์ ๋ฅผ ์ดํดํ์ง ๋ชปํ๊ณ 3.0 ๋ฆด๋ฆฌ์ค ๋
ธํธ์ ๋ชจ์๋๋ ๊ฒ์ ์ ์ํ๊ธฐ ๋๋ฌธ์
๋๋ค.
@RyanCavanaugh ๋ด์ด์ด ๋ฌธ์ ๋ฅผ ๋ซ์ ์ด์ ๋ ๋ฌด์์ ๋๊น?
๋์น ๊ฒ์ผ ์ ์์ง๋ง ๋ง์ ํด๊ฒฐ ๋ฐฉ๋ฒ์๋ ๋ถ๊ตฌํ๊ณ ์ค์ ๋ต๋ณ์ ๋ณผ ์ ์์ต๋๊น?
์ถ์ : TypeScript v4 ์ฌ์ฉ
์ด๋ฐ ์์ผ๋ก ํ ์ ์์ต๋๋ค. ์ฌ๊ธฐ์ Props์ ์์ฑ์ ์ ํ์ ๋งค๊ฐ ๋ณ์์ด๋ฉฐ ๊ตฌ์ฑ ์์์์ ์ถ๊ฐ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
interface Props {
children: ReactNode;
properties?: Record<string, unknown>;
}
const defaultProps: Props = {
children: '',
properties: {
marginTop: 32,
marginLeft: 0,
marginBottom: 8,
marginRight: 0,
},
};
const RadioListLabel: React.FC<Props> = ({
children,
properties = defaultProps.properties,
}) => (
<View
margin={[
properties ? properties.marginTop : 0
properties ? properties.marginLeft : 0
properties ? properties.marginBottom : 0
properties ? properties.marginRight : 0
]}
>
<Text size="m" variant="heading2" lineHeight="body">
{children}
</Text>
</View>
)
@lakhmeranikita ์ด ๋ฌธ์ ์ ๋ชฉ์ ์ ์ํ ์ ํ์ ์ ํ ์ฌํญ์ผ๋ก ํ์ํ์ง ์๋ ๊ฒ์ ๋๋ค.
ํจ์ ๊ธฐ๋ณธ ์ธ์ ๋๋ ๋ค๋ฅธ ์ ์์ ์ ๋๋ก ์ ๋ ฅํ์ง ๋ชปํ์ต๋๋ค. ๋ด๊ฐ ์ฐพ์ ์ ์ผํ ํด๊ฒฐ์ฑ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import React from "react";
type DefaultProps = { optionalProp: number };
type Props = {
requiredProp: "some_string" | "other_string";
} & DefaultProps;
export const BaseComponent = (props: Props) => {
const { requiredProp, optionalProp } = props;
return (
<div>
{requiredProp} {optionalProp}
</div>
);
};
BaseComponent.defaultProps = {
optionalProp: 0,
} as Partial<DefaultProps>;
const comp = <BaseComponent requiredProp="some_string" />;
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
TypeScript 3.1 ์ด์์์๋ ๋ค์์ ์์ฑํ ์ ์์ด์ผํฉ๋๋ค.