Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 反应道具类型:允许一个道具使用不同类型的道具类型_Reactjs_React Proptypes - Fatal编程技术网

Reactjs 反应道具类型:允许一个道具使用不同类型的道具类型

Reactjs 反应道具类型:允许一个道具使用不同类型的道具类型,reactjs,react-proptypes,Reactjs,React Proptypes,我有一个组件,根据其大小接收道具。道具可以是字符串或数字,例如:“大”或17 我是否可以让作出反应。PropTypes知道这可以是PropTypes验证中的一个或另一个 如果未指定类型,则会收到警告: 道具类型尺寸无效;它必须是一个函数,通常来自 反应 了解更多信息:从“React”导入React 这可能适合您: height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), 出于文档目的,最好列出合法的字符串值: size

我有一个组件,根据其大小接收道具。道具可以是字符串或数字,例如:
“大”
17

我是否可以让
作出反应。PropTypes
知道这可以是PropTypes验证中的一个或另一个

如果未指定类型,则会收到警告:

道具类型
尺寸
无效;它必须是一个函数,通常来自
反应


了解更多信息:

从“React”导入React 这可能适合您:

height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

出于文档目的,最好列出合法的字符串值:

size: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.oneOf([ 'SMALL', 'LARGE' ]),
]),

下面是使用多proptype和单proptype的pro示例

import React, { Component } from 'react';
import { string, shape, array, oneOfType } from 'prop-types';

class MyComponent extends Component {
  /**
   * Render
   */
  render() {
    const { title, data } = this.props;

    return (
      <>
        {title}
        <br />
        {data}
      </>
    );
  }
}

/**
 * Define component props
 */
MyComponent.propTypes = {
  data: oneOfType([array, string, shape({})]),
  title: string,
};

export default MyComponent;

import React,{Component}来自'React';
从“道具类型”导入{string,shape,array,oneOfType};
类MyComponent扩展组件{
/**
*渲染
*/
render(){
const{title,data}=this.props;
返回(
{title}

{data} ); } } /** *定义组件道具 */ MyComponent.propTypes={ 数据:一种类型([array,string,shape({})], 标题:字符串, }; 导出默认MyComponent;
是的,PropTypes现在存在于它自己的包中,但这并不能回答问题…与问题完全不相关感谢这一点,我在运行Jest测试时在类中设置静态PropTypes时会出现随机错误:
ReferenceError:oneOfType未定义
-有什么建议吗?提前谢谢!!你确定你已经正确地从“道具类型”导入了
道具类型吗?嘿,帕维尔——是的,我们就是这样导入它们的:
从“道具类型”导入道具类型2019-使用PropType。oneOf@Imdad:
oneOf
不用于类型检查,仅用于限制属性的实际值。因此,继续使用
PropTypes.oneOfType
。虽然此代码可以回答问题,但提供有关此代码回答问题的原因和/或方式的附加上下文可以提高其长期价值。这是在prop应为枚举时执行此操作的方法。您甚至可以为
oneOf
方法提供枚举。
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
size: PropTypes.oneOfType([
    PropTypes.number,
    PropTypes.oneOf([ 'SMALL', 'LARGE' ]),
]),
import React, { Component } from 'react';
import { string, shape, array, oneOfType } from 'prop-types';

class MyComponent extends Component {
  /**
   * Render
   */
  render() {
    const { title, data } = this.props;

    return (
      <>
        {title}
        <br />
        {data}
      </>
    );
  }
}

/**
 * Define component props
 */
MyComponent.propTypes = {
  data: oneOfType([array, string, shape({})]),
  title: string,
};

export default MyComponent;