Reactjs 如何在连接的组件上实施道具类型?

Reactjs 如何在连接的组件上实施道具类型?,reactjs,redux,react-redux,prop,Reactjs,Redux,React Redux,Prop,在这件事上我的头撞到了墙上。我有一个用于语言翻译的组件,它连接到我的redux存储,在那里保存语言值。它获得一个语言键,并呈现模糊的占位符文本,直到语言值进入状态,在这种情况下,它呈现请求的字符串。我试图将此作为几个组件的道具的唯一道具类型来执行。但我一直看到错误 import React from "react"; import PropTypes from 'prop-types'; import { connect } from "react-redux"; import { dummyT

在这件事上我的头撞到了墙上。我有一个用于语言翻译的组件,它连接到我的redux存储,在那里保存语言值。它获得一个语言键,并呈现模糊的占位符文本,直到语言值进入状态,在这种情况下,它呈现请求的字符串。我试图将此作为几个组件的道具的唯一道具类型来执行。但我一直看到错误

import React from "react";
import PropTypes from 'prop-types';
import { connect } from "react-redux";
import { dummyText } from "../../constants/placeholderDummyText";
import { substrByWordCount } from "../../utilities/strings";

// displays blurred dummy text if placeholder is supplied, else displays the language value for the key provided
export const TextValue = ({ language, langKey, placeholderWordCount = 1 }) => {

    const hasLanguageAvailable = Object.entries(language).length !== 0;

    const placeholder = substrByWordCount(dummyText, placeholderWordCount);

    return hasLanguageAvailable ? (
        <span dangerouslySetInnerHTML={{
            __html: language[langKey]
        }} />
    ) : (
            <span className='blurredText'>
                {placeholder}
            </span>
        );
};

TextValue.propTypes = {
    /**
     * The key of the language value to retrieve
     */
    langKey: PropTypes.string.isRequired,
    /**
     * How many blurred words to surface as placeholder elements while waiting for language to flow in
     */
    placeholderWordCount: PropTypes.number
}


function mapStateToProps(state) {
    return {
        language: state.language.values
    };
};

export default connect(mapStateToProps)(TextValue);
这会导致React出现错误:

警告:失败的道具类型:为“TextInput”提供的类型为“Object”的道具“title”无效,应为“TextValue”的实例。


是否可以检查我的TextValue导出的类型?由于它是一个功能组件,它是否需要一种不同的方法将其强制作为一个属性类型?

尝试
PropTypes.element
(对于React元素,即
)或
PropTypes.elementType
(对于React元素类型,即
MyComponent
)而不是
属性类型。instanceOf

/。。。
标签:PropTypes.elementType.isRequired,
// ...
PropTypes.instanceOf
用于JS类的实例,而不用于React组件


请参阅。

这很有帮助。有没有办法检查元素的类型?“elementType”让我更进一步,但我只想验证一种特定类型的元素。编辑:看起来“elementType”也会引发错误:
失败的属性类型:提供给“TextInput”的类型为“object”的属性“title”无效,应为单个元素类型。
我认为无法验证特定组件类型(请检查此处的文档:)。我不知道你传递给道具的是什么,是反应元素还是反应元素类型。尝试
PropTypes.element
。请参阅我编辑的答案。
TextInput.propTypes = {
    /**
     * The name of the form input.
     */
    name: PropTypes.string.isRequired,
    /**
     * The placeholder text to display within the input field. Gets its value from the input name if not supplied.
     */
    placeholder: PropTypes.string,
    /**
     * The input's label.
     */
    label: PropTypes.instanceOf(TextValue).isRequired,
    /**
     * Represents whether or not the input is required in order to submit the form to which it belongs
     */
    required: PropTypes.bool
}