Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36199

Type checking with react v15.5. React.PropTypes is deprecated

When I go to React documentation page I see that now there is no guide about typechecking with PropTypes. Then I open guide about react context and I see message that states I should now use prop-types library. But this library has no documentation. I would like to know how to use it with React.

Upvotes: 2

Views: 285

Answers (2)

Dan Prince
Dan Prince

Reputation: 29999

To reduce the size of React's core, prop types have been moved out into their own package (prop-types).

The new prop-types module works in exactly the same way as the original React one, except now you'll access the PropTypes object by importing it, rather than as a property of React.

import PropTypes from 'prop-types';

myComponent.propTypes = {
  foo: PropTypes.string
};

// rather than

myComponent.propTypes = {
  foo: React.PropTypes.string
};

This deprecation warning only landed in React v15.5, which was released yesterday. I imagine the documentation will stabilise before too long.

For now, the current documentation is still fine, as long as you follow the rule above.

Upvotes: 1

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40308

Although this question is kind of off-topic for Stack Overflow, it give me a heads-up for this change, so thanks :) I found an article explaining the change here. Quoting:

[...] instead of accessing PropTypes from the main React object, install the prop-types package and import them from there:

import React from 'react';
import PropTypes from 'prop-types';      //// CHANGE (1) HERE

class Component extends React.Component {
  [...]
}
Component.propTypes = {
  text: PropTypes.string.isRequired      //// CHANGE (2) HERE
}

Upvotes: 2

Related Questions