简体   繁体   中英

prop-types doesn't work in React Native App

PropTypes doesn't work for me in React Native. I think the code is fine but this way RN doesn't warn me about Prop-types violations and defaults props doesn't even works, I don't know why.

My npm packages: "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 };

should be:

ModalAlert.propTypes = {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM