属性类型是必需的未定义



我正在学习reactjs,我想使用PropType,在一些例子中,我看到了类似PropTypes.string.isRequired的东西。但是如果我写这个,我会收到一个错误,即"必需"未定义。

import React from "react";
import PropTypes from "prop-type";
import classnames from "classnames";
const TextFieldGroup = ({ field, value, label, error, type, onChange }) => {
return (
<div className={classnames("form-group", { "has-error": error })}>
<label className="control-label">{label}</label>
<input
value={value}
onChange={onChange}
type={type}
name={field}
className="form-control"
/>
{error && <span className="help-block">{error}</span>}
</div>
);
};
TextFieldGroup.propTypes = {
field: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired,
error: PropTypes.string,
type: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
checkUserExists: PropTypes.func.isRequired
};
TextFieldGroup.defaultProps = {
type: "text"
};
export default TextFieldGroup;

希望有人能帮忙,谢谢!

它是道具类型,但不是道具类型。您正在导入道具类型,而不是道具类型。正确的模块是道具类型

所以你需要安装

npm i —save prop-types

然后像导入一样

import PropTypes from "prop-types"

最新更新