首页 > 解决方案 > prop-types 在 React Native App 中不起作用

问题描述

PropTypes 在 React Native 中对我不起作用。我认为代码很好,但是这样 RN 不会警告我关于 Prop-types 违规和默认道具甚至不起作用,我不知道为什么。

我的 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 };

标签: javascriptreactjsreact-nativereact-proptypes

解决方案


应该:

ModalAlert.propTypes = {}

推荐阅读