Mar*_*ark 0 javascript prop node.js reactjs react-proptypes
我正在使用 react.js 构建一个表单,但遇到了一个我似乎无法解决的问题。prop-types我在表单中为我的道具使用 npm 模块并尝试inputType为单个输入设置一个。我不断收到以下错误:
我的代码是:
import React from 'react';
import PropTypes from 'prop-types';
const SingleInput = (props) => (
<div className="form-group">
<label className="form-label">{props.title}</label>
<input
className="form-input"
name={props.name}
type={props.inputType}
value={props.content}
onChange={props.controlFunc}
placeholder={props.placeholder} />
</div>
);
SingleInput.propTypes = {
inputType: React.PropTypes.oneOfType(['text', 'number']).isRequired,
title: React.PropTypes.string.isRequired,
name: React.PropTypes.string.isRequired,
controlFunc: React.PropTypes.func.isRequired,
content: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]).isRequired,
placeholder: React.PropTypes.string,
};
export default SingleInput;
Run Code Online (Sandbox Code Playgroud)
我在使用oneOf和 then之间切换oneOfType,但无论哪种方式都得到相同的结果。还有什么我应该做的吗?
非常感谢。
我相信你使用 React v16 吗?您可以在他们的发行说明中阅读他们PropTypes从react软件包中删除的信息。
15.x 中引入的弃用已从核心包中删除。React.createClass 现在可以作为 create-react-class,React.PropTypes 作为 prop-types,React.DOM 作为 react-dom-factories,react-addons-test-utils 作为 react-dom/test-utils,以及浅渲染器作为反应测试渲染器/浅层
您应该直接使用该prop-types库:
SingleInput.propTypes = {
inputType: PropTypes.oneOfType(['text', 'number']).isRequired,
title: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
controlFunc: PropTypes.func.isRequired,
content: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]).isRequired,
placeholder: PropTypes.string,
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1863 次 |
| 最近记录: |