我有一个组件<Button>

100 prop not required:

<Button>Add to bag</Button>

100 prop has to be required:

<Button ariaLabel="Add to bag" icon={ favorite } />

if 100 and 101 are empty, it throws an error saying that 101 is 103

<Button icon={ favorite } />

propTypes:

Button.propTypes = {
    /** icon inside Button. */
    icon: React.PropTypes.object,
    /** Content inside button */
    children: React.PropTypes.node,
    /** Aria-label to screen readers */
    ariaLabel: React.PropTypes.string, /*isRequired if children is empty */
};

谢谢

推荐答案

你不需要另一个库,"props 类型"提供了开箱即用的功能.

例子:

import PropTypes from 'prop-types';

//.......    

ExampleComponent.propTypes = {
    showDelete: PropTypes.bool,
    handleDelete: function(props, propName, componentName) {
        if ((props['showDelete'] == true && (props[propName] == undefined || typeof(props[propName]) != 'function'))) {
            return new Error('Please provide a handleDelete function!');
        }
    },

}

Reactjs相关问答推荐

使用useEffect挂钩获取数据

包装组件可以避免子组件重新渲染.?

使用Reaction钩子窗体验证可选字段

为什么登录错误和密码在创建Reaction-Hook-Form后立即被删除?

在动态React组件中删除事件侦听器

在transformResponse中使用来自其他查询的缓存

无法在REACTION TANSTACK查询中传递参数

ctx.strokeStyle 在 canvas html 5 中不起作用

如何删除 bootstrap 手风琴上的边框

React 错误无法读取未定义的属性(读取id)#react

标签文本删除了 MUI 概述的输入

无法使axiosmockadapter正常工作

如何在next.js 13中仅在主页导航栏上隐藏组件?

在 CrafterCMS Studio 中拖动字段

Recharts 笛卡尔网格 - 垂直线和水平线的不同样式

从服务器获取数据时未定义,错误非法调用

为什么 setState 在 React 中不是异步的?

DatePicker MUI 如何在日历左侧显示清除文本

单击按钮时获取react 表中每一行的属性

在状态中存储多个选定的选项