使用 React v15.5 进行类型检查。 React.PropTypes 已弃用
Type checking with react v15.5. React.PropTypes is deprecated
当我转到 React 文档页面时,我看到现在没有关于使用 PropTypes 进行类型检查的指南。然后我打开关于 react context and I see message that states I should now use prop-types library 的指南。但是这个库没有文档。我想知道如何将它与 React 一起使用。
虽然这个问题对于 Stack Overflow 来说有点离题,但它让我对这个变化有所了解,所以谢谢 :) 我找到了一篇解释这个变化的文章 here。引用:
[...] 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
}
为了减小 React 核心的大小,prop 类型已移出到它们自己的包中 (prop-types)。
新的 prop-types 模块的工作方式与原来的 React 模块完全相同,除了现在您将通过导入来访问 PropTypes
对象,而不是作为 属性反应。
import PropTypes from 'prop-types';
myComponent.propTypes = {
foo: PropTypes.string
};
// rather than
myComponent.propTypes = {
foo: React.PropTypes.string
};
这个弃用警告只出现在昨天发布的 React v15.5 中。我想文档很快就会稳定下来。
目前,只要您遵循上述规则,当前文档仍然可以。
当我转到 React 文档页面时,我看到现在没有关于使用 PropTypes 进行类型检查的指南。然后我打开关于 react context and I see message that states I should now use prop-types library 的指南。但是这个库没有文档。我想知道如何将它与 React 一起使用。
虽然这个问题对于 Stack Overflow 来说有点离题,但它让我对这个变化有所了解,所以谢谢 :) 我找到了一篇解释这个变化的文章 here。引用:
[...] 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 }
为了减小 React 核心的大小,prop 类型已移出到它们自己的包中 (prop-types)。
新的 prop-types 模块的工作方式与原来的 React 模块完全相同,除了现在您将通过导入来访问 PropTypes
对象,而不是作为 属性反应。
import PropTypes from 'prop-types';
myComponent.propTypes = {
foo: PropTypes.string
};
// rather than
myComponent.propTypes = {
foo: React.PropTypes.string
};
这个弃用警告只出现在昨天发布的 React v15.5 中。我想文档很快就会稳定下来。
目前,只要您遵循上述规则,当前文档仍然可以。