G. Frx
G. Frx

Reputation: 3090

React Js PropTypes required without type

I'm implementing a reusable Input component with React (ES6). I want to make the props "value" generic (like without defining a type).

For now, type is set to string but I don't want that since the Input component must handle number / string / password / etc

value: React.PropTypes.string

And I need something like

value: React.PropTypes,

Which is not possible. Is someone know a elegant solution ?

Thanks in advance.

Upvotes: 3

Views: 961

Answers (2)

Incinerator
Incinerator

Reputation: 2807

While iamsaksham's answer is technically correct, it would probably be a better idea to do something like this:

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

This will give you much better control, as PropTypes.any doesn't really add much type safety to your code.

(Also, React.PropTypes has been moved to a separate package so you need to do import PropTypes from 'prop-types'; nowadays)

See the docs for more info.

Upvotes: 2

iamsaksham
iamsaksham

Reputation: 2949

You can use requiredAny: React.PropTypes.any which is a value of any datatype.

Upvotes: 3

Related Questions