reactjs - React Hooks - 具有解构数组变量的 JSDoc
问题描述
我正在尝试 JSDoc 一个带有钩子的简单 React Typescript 组件。不幸的是,我似乎找不到使 JSDoc 与声明的解构数组一起工作的方法。有一些与解构对象参数相关的 答案,但这些不适用于数组。
/**
* @property {boolean} 0 - documentation for isLoading
* @property {func} 1 - documentation for setIsLoading
*/
const [isLoading, setIsLoading] = React.useState<boolean>(false);
更新 1:仍然无法找到记录这种解构的方法。有一个极端情况,如果我自定义类型一个对象,它可以工作:
export type AuthFormInput = {
/** Value of the Email input field */
email: string;
/** Value of the Password input field */
password: string;
};
const [form, setForm] = React.useState<AuthFormInput>({
email: '',
password: ''
});
...
// JSDoc will work here
const email = form.email;
解决方案
这会有所帮助:
/**
* @typedef {Boolean} LoadingState — documentation for isLoading
* @description Additional doc
*/
/**
* @typedef {Function} LoadingStateSetter — documentation for setIsLoading
*/
/**
* @type {[LoadingState, LoadingStateSetter]} Loading
*/
const [isLoading, setIsLoading] = React.useState();
在这个例子中,我们声明了两个额外的类型:LoadingState
和LoadingStateSetter
。然后我们为它们添加一些描述,最后,我们声明Loading
结果的类型React.useState()
您也可以像这样以更简单的方式声明它:
/**
* @type {[Boolean, Function]} Loading
*/
const [isLoading, setIsLoading] = React.useState();
但是在这种情况下,我没有找到添加描述的方法。
我已经在 VSCode 中检查了这种文档方式的描述
推荐阅读
- azure - 如何在 Azure Logic App 中解码 JWT 令牌?
- arrays - 通过为列表设置新值来映射更改的字段
- javascript - 从父控制器调用 $mdDialog 内的函数
- java - io.netty HTTP 服务器的 SSL 证书
- vuejs2 - 在 vuejs 代码中按函数分组求和
- python - flask-migrate/alembic 有没有办法列出已应用于数据库的迁移?
- javascript - Popper.js 工具提示偏离中心/定位错误
- google-chrome - 如何自动将域用户登录到 Azure DevOps Server 2019?
- angular - subscribe 里面的代码被执行了多次
- react-native - touchableopacity 反应原生 z 索引