简体   繁体   English

React表单输入类型中的PropType错误

[英]PropType error in React form input type

I am building a form with react.js and having an issue I cannot seem to resolve. 我正在用react.js构建表单,但有一个我似乎无法解决的问题。 Am using the npm module prop-types for my props within the form and trying to set an inputType for a single input. 我在表单中为道具使用npm模块prop-types ,并尝试为单个输入设置inputType I keep getting the following error: 我不断收到以下错误:

在此处输入图片说明

My code for this is: 我的代码是:

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;

I have switched between using oneOf and then oneOfType , but get the same results either way. 我一直在使用之间切换oneOf然后oneOfType ,但得到相同的结果无论哪种方式。 Is there something further I should do? 还有什么我应该做的吗?

Thanks much. 非常感谢。

I believe you use React v16 ? 相信您使用React v16? You can read in their release note that they removed PropTypes from their react package. 你可以在自己的发行说明中读到他们取消PropTypes从他们的react包。

The deprecations introduced in 15.x have been removed from the core package. 15.x中引入的弃用已从核心软件包中删除。 React.createClass is now available as create-react-class, React.PropTypes as prop-types, React.DOM as react-dom-factories, react-addons-test-utils as react-dom/test-utils, and shallow renderer as react-test-renderer/shallow React.createClass现在可以作为create-react-class使用,React.PropTypes作为prop-types,React.DOM作为react-dom-factories,react-addons-test-utils作为react-dom / test-utils,以及浅层渲染器作为react-test-renderer / shallow

You should directly use the prop-types library: 您应该直接使用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,
};

oneOfType means that you expect the variable to be one of a specific prop-type, while oneOf can be any self choosen value. oneOfType表示您希望变量是特定的prop-type之一,而oneOf可以是任何自行选择的值。 Here an example how both would look like. 这里有一个例子,两者看起来如何。

Game.propTypes = {      
  gameStatus: PropTypes.oneOf(['new_game', 'playing', 'finished']).isRequired,
  roundsToPlay: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
}

I came here because i had a problem with a <input type="number" /> element. 我来到这里是因为我对<input type="number" />元素有疑问。 I wanted to have the value as props, but every time the user would remove default value to type in a new one i would get a warning from prop-types. 我希望将值用作道具,但是每次用户删除默认值以键入新值时,道具类型都会发出警告。 As the value is now an empty string instead of any given number. 由于该值现在是一个空字符串,而不是任何给定的数字。 The solution is to set the prop-type with oneOfTypes to accept string and number. 解决方案是使用oneOfTypes设置prop-type以接受字符串和数字。

I hope i could help someone with this additional information with the same problem on number inputs. 我希望我可以为某人提供这些附加信息,同时解决数字输入方面的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM