PropTypes์ ํจ๊ป ํ๋ ํ์ ๊ฒ์ฌ
These docs are old and wonโt be updated. Go to react.dev for the new React docs.
PropTypes arenโt commonly used in modern React. Use TypeScript for static type checking.
์ฃผ์
React.PropTypes
๋ React v15.5๋ถํฐ ๋ค๋ฅธ ํจํค์ง๋ก ์ด๋ํ์์ต๋๋ค. ๋์prop-types
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์๊ธธ ๋ฐ๋๋๋ค.์ฐ๋ฆฌ๋ ๋ณํ์ ์๋ํํ๊ธฐ ์ํ์ฌ codemod ์คํฌ๋ฆฝํธ๋ฅผ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
์ฌ๋ฌ๋ถ์ ์ฑ์ด ์ปค์ง์ ๋ฐ๋ผ ํ์ ๊ฒ์ฌ๋ฅผ ํ์ฉํ๋ฉด ๋ง์ ๋ฒ๊ทธ๋ฅผ(bug) ์ก์ ์ ์์ต๋๋ค. ํน์ ์ ํ๋ฆฌ์ผ์ด์ ์์๋ ์ ์ฒด ์ ํ๋ฆฌ์ผ์ด์ ์ ํ์ ๊ฒ์ฌ๋ฅผ ์ํด Flow ๋๋ TypeScript์ ๊ฐ์ JavaScript ๋๊ตฌ(Extensions)๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฌํ ๊ฒ๋ค์ ์ฌ์ฉํ์ง ์๋๋ผ๋ React๋ ๋ด์ฅ๋ ํ์ ๊ฒ์ฌ ๊ธฐ๋ฅ๋ค์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ์ปดํฌ๋ํธ์ props์ ํ์ ๊ฒ์ฌ๋ฅผ ํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํน๋ณํ ํ๋กํผํฐ์ธ propTypes๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค.
import PropTypes from 'prop-types';
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
}
Greeting.propTypes = {
name: PropTypes.string
};
์์์์ ํด๋์ค ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๊ณ ์์ง๋ง ํจ์ ์ปดํฌ๋ํธ์ React.memo
, React.forwardRef
๋ฅผ ํตํด ๋ง๋ค์ด์ง ์ปดํฌ๋ํธ์๋ ๋์ผํ๊ฒ ์ ์ฉ๋ ์ ์์ต๋๋ค.
PropTypes
๋ ์ ๋ฌ๋ฐ์ ๋ฐ์ดํฐ์ ์ ํจ์ฑ์ ๊ฒ์ฆํ๊ธฐ ์ํด์ ๋ค์ํ ์ ํจ์ฑ ๊ฒ์ฌ๊ธฐ(Validator)๋ฅผ ๋ด๋ณด๋
๋๋ค. ์๋ ์์์์๋ PropTypes.string
์ ์ฌ์ฉํ๊ฒ ๋ ๊ฒ์
๋๋ค. prop์ ์ ํจํ์ง ์์ ๊ฐ์ด ์ ๋ฌ ๋์์ ๋, ๊ฒฝ๊ณ ๋ฌธ์ด JavaScript ์ฝ์์ ํตํด ๋ณด์ผ ๊ฒ์
๋๋ค. propTypes
๋ ์ฑ๋ฅ์์ ์ด์ ๋ก ๊ฐ๋ฐ ๋ชจ๋(Development mode) ์์๋ง ํ์ธ๋ ๊ฒ์
๋๋ค.
PropTypes
์๋๋ ์ ๊ณต๋ ์๋ก ๋ค๋ฅธ ์ ํจ์ฑ ๊ฒ์ฌ๊ธฐ๋ค์ ๋ณด์ฌ์ฃผ๋ ์์์ ๋๋ค.
import PropTypes from 'prop-types';
MyComponent.propTypes = {
// prop๊ฐ ํน์ JS ํ์์์ ์ ์ธํ ์ ์์ต๋๋ค.
// ์ด๊ฒ๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ์ ํ ์ฌํญ์
๋๋ค.
optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
optionalNumber: PropTypes.number,
optionalObject: PropTypes.object,
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,
// ๋ ๋๋ง ๋ ์ ์๋ ๊ฒ๋ค์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
// ์ซ์(numbers), ๋ฌธ์(strings), ์๋ฆฌ๋จผํธ(elements), ๋๋ ์ด๋ฌํ ํ์
๋ค(types)์ ํฌํจํ๊ณ ์๋ ๋ฐฐ์ด(array) (ํน์ ๋ฐฐ์ด์ fragment)
optionalNode: PropTypes.node,
// React ์๋ฆฌ๋จผํธ.
optionalElement: PropTypes.element,
// React ์๋ฆฌ๋จผํธ ํ์
(ie. MyComponent)
optionalElementType: PropTypes.elementType,
// prop๊ฐ ํด๋์ค์ ์ธ์คํด์ค์์ ์ ์ธํ ์ ์์ต๋๋ค.
// ์ด ๊ฒฝ์ฐ JavaScript์ instanceof ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
optionalMessage: PropTypes.instanceOf(Message),
// ์ด๊ฑฐํ(enum)์ผ๋ก ์ฒ๋ฆฌํ์ฌ prop๊ฐ ํน์ ๊ฐ๋ค๋ก ์ ํ๋๋๋ก ํ ์ ์์ต๋๋ค.
optionalEnum: PropTypes.oneOf(['News', 'Photos']),
// ์ฌ๋ฌ ์ข
๋ฅ์ค ํ๋์ ์ข
๋ฅ๊ฐ ๋ ์ ์๋ ๊ฐ์ฒด
optionalUnion: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Message)
]),
// ํน์ ํ์
์ ํ๋ ฌ
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
// ํน์ ํ์
์ ํ๋กํผํฐ ๊ฐ๋ค์ ๊ฐ๋ ๊ฐ์ฒด
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
// ํน์ ํํ๋ฅผ ๊ฐ๋ ๊ฐ์ฒด
optionalObjectWithShape: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number
}),
// ์ถ๊ฐ ํ๋กํผํฐ์ ๋ํ ๊ฒฝ๊ณ ๊ฐ ์๋ ๊ฐ์ฒด
optionalObjectWithStrictShape: PropTypes.exact({
name: PropTypes.string,
quantity: PropTypes.number
}),
// ์์ ์๋ ๊ฒ ๋ชจ๋ `isRequired`์ ์ฐ๊ฒฐํ์ฌ prop๊ฐ ์ ๊ณต๋์ง ์์์ ๋
// ๊ฒฝ๊ณ ๊ฐ ๋ณด์ด๋๋ก ํ ์ ์์ต๋๋ค.
requiredFunc: PropTypes.func.isRequired,
// ๋ชจ๋ ๋ฐ์ดํฐ ํ์
์ด ๊ฐ๋ฅํ ํ์๊ฐ
requiredAny: PropTypes.any.isRequired,
// ์ฌ์ฉ์ ์ ์ ์ ํจ์ฑ ๊ฒ์ฌ๊ธฐ๋ฅผ ์ง์ ํ ์๋ ์์ต๋๋ค.
// ๊ฒ์ฌ ์คํจ ์์๋ ์๋ฌ(Error) ๊ฐ์ฒด๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
// `oneOfType`์์์๋ ์๋ํ์ง ์์ผ๋ฏ๋ก `console.warn` ํน์ throw ํ์ง ๋ง์ธ์.
customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
'Invalid prop `' + propName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
},
// `arrayOf` ์ `objectOf ์ ์ฌ์ฉ์ ์ ์ ์ ํจ์ฑ ๊ฒ์ฌ๊ธฐ๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค.
// ๊ฒ์ฌ ์คํจ ์์๋ ์๋ฌ(Error) ๊ฐ์ฒด๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
// ์ ํจ์ฑ ๊ฒ์ฌ๊ธฐ๋ ๋ฐฐ์ด(array) ํน์ ๊ฐ์ฒด์ ๊ฐ ํค(key)์ ๋ํ์ฌ ํธ์ถ๋ ๊ฒ์
๋๋ค.
// ์ ํจ์ฑ ๊ฒ์ฌ๊ธฐ์ ์ฒซ ๋ ๊ฐ์ ๋ณ์๋ ๋ฐฐ์ด ํน์ ๊ฐ์ฒด ์์ ๊ณผ ํ์ฌ ์์ดํ
์ ํค์
๋๋ค.
customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
'Invalid prop `' + propFullName + '` supplied to' +
' `' + componentName + '`. Validation failed.'
);
}
})
};
ํ๋์ ์์๋ง ์๊ตฌํ๊ธฐ
PropTypes.element
๋ฅผ ์ด์ฉํ์ฌ ์ปดํฌ๋ํธ์ ์์๋ค(Children)์ ๋จ ํ๋์ ์์(Child)๋ง์ด ์ ๋ฌ๋ ์ ์๋๋ก ๋ช
์ํ ์ ์์ต๋๋ค.
import PropTypes from 'prop-types';
class MyComponent extends React.Component {
render() {
// ์ด๊ฒ์ ๋ฐ๋์ ํ๋์ ์๋ฆฌ๋จผํธ์ฌ์ผ ํฉ๋๋ค. ์๋๋ผ๋ฉด, ๊ฒฝ๊ณ (warn)๊ฐ ์ผ์ด๋ ๊ฒ์
๋๋ค.
const children = this.props.children;
return (
<div>
{children}
</div>
);
}
}
MyComponent.propTypes = {
children: PropTypes.element.isRequired
};
์ด๊ธฐ Prop ๊ฐ
defaultProps
ํ๋กํผํฐ๋ฅผ ํ ๋นํจ์ผ๋ก์จ props
์ ์ด๊น๊ฐ์ ์ ์ํ ์ ์์ต๋๋ค.
class Greeting extends React.Component {
render() {
return (
<h1>Hello, {this.props.name}</h1>
);
}
}
// props์ ์ด๊น๊ฐ์ ์ ์ํฉ๋๋ค.
Greeting.defaultProps = {
name: 'Stranger'
};
// Renders "Hello, Stranger":
const root = ReactDOM.createRoot(document.getElementById('example'));
root.render(<Greeting />);
Since ES2022 you can also declare defaultProps
as static property within a React component class. For more information, see the class public static fields. This modern syntax will require a compilation step to work within older browsers.
class Greeting extends React.Component {
static defaultProps = {
name: 'stranger'
}
render() {
return (
<div>Hello, {this.props.name}</div>
)
}
}
defaultProps
๋ this.props.name
์ ๊ฐ์ด ๋ถ๋ชจ ์ปดํฌ๋ํธ์ ์ํด ๋ช
์๋์ง ์์์ ๋ ๊ฐ์ ๊ฐ๋๋ก ํ ๊ฒ์
๋๋ค. propTypes
์ ํ์
๊ฒ์ฌ๋ defaultProps
์๋ ์ ์ฉ๋๊ฒ ํ๊ธฐ ์ํ์ฌ defaultProps
๊ฐ ์ฒ๋ฆฌ๋ ๋ค์ ์ผ์ด๋ ๊ฒ์
๋๋ค.
Function Components
ํจ์ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํด์ ๊ฐ๋ฐํ๋ค๋ฉด, PropTypes๋ฅผ ์ ์ ํ ์ ์ฉํ ์ ์๋๋ก ๋ช ๊ฐ์ง ์์ ๋ณ๊ฒฝ์ฌํญ์ ๋ง๋ค์ด๋ผ ์๋ ์์ต๋๋ค.
์๋์ ๊ฐ์ ์ปดํฌ๋ํธ๊ฐ ์๋ค๊ณ ์๊ฐํด๋ด ์๋ค.
export default function HelloWorldComponent({ name }) {
return (
<div>Hello, {name}</div>
)
}
PropTypes๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด ์๋์ฒ๋ผ ์ปดํฌ๋ํธ๋ฅผ ์ธ๋ถ์ ๋ ธ์ถ์ํค๊ธฐ ์ ์ ๋ณ๋์ ํจ์๋ก ์ปดํฌ๋ํธ๋ฅผ ์ ์ธํ ์ ์์ต๋๋ค.
function HelloWorldComponent({ name }) {
return (
<div>Hello, {name}</div>
)
}
export default HelloWorldComponent
๊ทธ๋ฌ๋ฉด, HelloWorldComponent
์ ์ง์ PropTypes๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค.
import PropTypes from 'prop-types'
function HelloWorldComponent({ name }) {
return (
<div>Hello, {name}</div>
)
}
HelloWorldComponent.propTypes = {
name: PropTypes.string
}
export default HelloWorldComponent