PropTypes在React Native中对我不起作用。我认为代码很好,但是RN不会向我警告有关Prop类型违规的信息,并且默认props甚至无法工作,我不知道为什么。
我的npm软件包: “ prop-types”:“ ^ 15.7.2”, “ react”:“ 16.8.6”, “ react-native”:“ 0.60.4”
import React from "react";
import { Text } from "react-native";
import PropTypes from "prop-types";
import { Modal } from "@ant-design/react-native";
import config from "./config";
import styles from "./styles";
const ModalAlert = ({
title,
content,
okeyCallback,
cancelCallBack
}) => {
const renderTitle = title => <Text style={styles.title}>{title}</Text>;
const renderContent = content => (
<Text style={styles.content}>{content}</Text>
);
const renderBtn = text => <Text style={styles.btn}>{text}</Text>;
cancelCallBack
? Modal.alert(renderTitle(title), renderContent(content), [
{
text: renderBtn(config.texts.confirmText),
onPress: () =>
okeyCallback === null ? console.log("Ok pressed") : okeyCallback()
},
{
text: renderBtn(config.texts.cancelText),
onPress: () =>
cancelCallBack === null
? console.log("Cancel pressed")
: cancelCallBack()
}
])
: Modal.alert(renderTitle(title), renderContent(content), [
{
text: renderBtn(config.texts.confirmText),
onPress: () =>
okeyCallback === null ? console.log("Ok pressed") : okeyCallback()
}
]);
};
ModalAlert.PropTypes = {
title: PropTypes.string.isRequired,
content: PropTypes.string.isRequired,
okeyCallback: PropTypes.func,
cancelCallBack: PropTypes.func
};
ModalAlert.defaultProps = {
title: "deafult"
};
export { ModalAlert };
答案 0 :(得分:2)
应为:
ModalAlert.propTypes = {}