React 和 JSDoc - 如何正确记录 React 组件?
我正在记录我的 React Native 组件,但我不知道如何正确执行。
对于文档生成,我使用jsdoc/better-docs,据说它能够收集您在 PropTypes
上留下的评论并将其包含在文档中。但是...由于不兼容问题,无法在 React Native 中执行此策略,因此 PropTypes
未包含在文档中。
如何使用 JSDOC 记录此 React 组件?
/**
* ??
*/
function Cat({ name, color = "#000" }) {
return <View />;
}
Cat.propTypes = {
name: PropTypes.string.isRequired,
color: PropTypes.string,
};
我正在执行以下操作:
/**
* The cat properties.
*
* @typedef {object} Props
* @property {string} name - The cat name.
* @property {string} [color="#000"] - The cat color.
*/
/**
* Cat component.
*
* @type {React.FC<Props>}
* @returns {React.ReactElement} The cat.
*/
function Cat({ name, color = "#000" }) {
return <View />;
}
Cat.propTypes = {
/** The cat name. */
name: PropTypes.string.isRequired,
/** The cat color. */
color: PropTypes.string,
};
但我觉得添加类型定义后 prop-types 毫无用处(?)。
你如何记录你的反应组件?
I am documenting my React Native components, but I don't know how to do it properly.
For the documentation generation, I am using jsdoc/better-docs, which is supposedly able to collect the comments you leave on your PropTypes
and include them in the documentation. But... due to incompatibility issues, it is not possible to carry out this strategy in React Native, and, therefore, the PropTypes
are not included in the documentation.
How do you document this React component using JSDOC?
/**
* ??
*/
function Cat({ name, color = "#000" }) {
return <View />;
}
Cat.propTypes = {
name: PropTypes.string.isRequired,
color: PropTypes.string,
};
I was doing the following:
/**
* The cat properties.
*
* @typedef {object} Props
* @property {string} name - The cat name.
* @property {string} [color="#000"] - The cat color.
*/
/**
* Cat component.
*
* @type {React.FC<Props>}
* @returns {React.ReactElement} The cat.
*/
function Cat({ name, color = "#000" }) {
return <View />;
}
Cat.propTypes = {
/** The cat name. */
name: PropTypes.string.isRequired,
/** The cat color. */
color: PropTypes.string,
};
But I am feeling that prop-types is useless after adding the type definitions (?).
How do you document your react components?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确的方法是使用
prop-types
中的InferProps
。此方法仅适用于 TypeScript :( 而且我没有使用它...相反,我在我的项目中结合了 JSDoc 和 PropTypes,以在开发体验中获得一些“打字稿行为”并自动生成我的文档。但有一个无需 TYPESCRIPT 的解决方法
只需按照我在此处描述的方式配置您的 JSDoc:JSDoc - 重用类型定义错误(找不到名称“类型名称” )
现在,在您的代码中,只需执行以下操作:
components/cat/propTypes.js:
components/cat/Cat.js:
现在一切正常就像一个魅力,没有理由维护无用的 JSDoc typedef:DDDDDD!
The way to go is to use
InferProps
fromprop-types
. This method is only available for TypeScript :( and I am not using it... instead, I am combining JSDoc and PropTypes in my project to get some "typescript behaviors" in the development experience and auto-generate my documentation.BUT THERE IS A WORKAROUND WITHOUT TYPESCRIPT
Just configure your JSDoc as I described here: JSDoc - reusing type definitions error (cannot find name ‘type name’)
Now, in your code, just do the following:
components/cat/propTypes.js:
components/cat/Cat.js:
Now everything is working like a charm and there is no reason to maintain useless JSDoc typedefs! :DDDDDD