【发布时间】:2023-03-25 17:03:02
【问题描述】:
我正在从 ReactJs 迁移到 React-Native,发现这个函数结构 in facebook's code for a react-native button:
class Button extends React.Component<{
title: string,
onPress: () => any,
color?: ?string,
hasTVPreferredFocus?: ?boolean,
accessibilityLabel?: ?string,
disabled?: ?boolean,
testID?: ?string,
}> {
static propTypes = {
/**
* Text to display inside the button
*/
title: PropTypes.string.isRequired,
/**
* Text to display for blindness accessibility features
*/
accessibilityLabel: PropTypes.string,
/**
* Color of the text (iOS), or background color of the button (Android)
*/
color: ColorPropType,
/**
* If true, disable all interactions for this component.
*/
disabled: PropTypes.bool,
/**
* TV preferred focus (see documentation for the View component).
*/
hasTVPreferredFocus: PropTypes.bool,
/**
* Handler to be called when the user taps the button
*/
onPress: PropTypes.func.isRequired,
/**
* Used to locate this view in end-to-end tests.
*/
testID: PropTypes.string,
};
...
}
在 ReactJS 中,我过去只是使用propTypes 检查来构建我的组件,所以:
a) 在类定义 ( 的括号内包含 props 规范的目的是什么?这在普通 ReactJS 中也可用吗?
b) 这会检查传递的属性格式吗?如果是这样,为什么我在这里需要 propTypes?
【问题讨论】:
标签: javascript reactjs react-native