首页 > 解决方案 > 访问作为参数传递的函数的参数

问题描述

我有一个反应组件,我需要验证其中一个道具应该是具有某种类型参数的函数。

我已经在我的设置中使用了道具类型,所以我虽然自定义验证器函数也可以帮助我验证这一点。

这就是我试图验证道具的功能签名的方式

children: (props, propName, componentName) => {
    let error;
    const prop = props[propName];
    if (typeof prop !== "function") {
      error = new Error("expected children to be a function");
    }

    // console.log(prop.length);
    if (prop.length === 0) {
      console.log(prop.length);
      error = new Error("expected children to be a function with 1 argument");
    }

    // don't know how to verify the type of arguments
    // can't use prop.arguments in strict mode

    return error;
  }

关于如何访问在 children 道具中传递的函数的参数的任何指针,然后验证它是否是typeof 'boolean'

标签: javascriptreactjsreact-proptypes

解决方案


推荐阅读