简体   繁体   English

我如何使用 JSDoc 记录 useState 挂钩?

[英]How do I document useState hooks with JSDoc?

I'm trying to use JSDoc to document the destructured parts of my react state hooks for example:我正在尝试使用 JSDoc 记录我的 react state 挂钩的解构部分,例如:

const [referenceState, setReferenceState] = useState(null);

Here, referenceState is of type Object, and setReferenceState expects an Object.这里, referenceState的类型是 Object,而setReferenceState需要一个 Object。

Based on some information online, I'm trying to do something along the lines of:根据网上的一些信息,我正在尝试按照以下方式做一些事情:

/**
* @param {Object} stateToSet
* @returns {GenericArray} current state and function to change value
*/
const [referenceState, setReferenceState] = useState(null);

But that doesn't generate anything..但这不会产生任何东西..

Can someone please help me document referenceState and setReferenceState ?有人可以帮我记录referenceStatesetReferenceState吗?

I think you can try this approach:我想你可以试试这个方法:

/**
 * @typedef {Object} ReferenceState
 */

/**
 * @callback ReferenceStateSetter
 * @param {ReferenceState} state
 * @returns {void}
 */

/**
 * @namespace {Object}
 * @property {ReferenceState} 0
 * @property {ReferenceStateSetter} 1 
 */
const [referenceState, setReferenceState] = useState(null);

Or, to avoid having to document the immediately destructured array, but benefiting from adding some indent changes at the end:或者,为了避免必须记录立即解构的数组,而是从最后添加一些缩进更改中受益:

/**
 * @typedef {Object} ReferenceState
 */

/**
 * @callback ReferenceStateSetter
 * @param {ReferenceState} state
 * @returns {void}
 */

const [
    /**
     * @type {ReferenceState}
     */
    referenceState,

    /**
     * @type {ReferenceStateSetter}
     */
    setReferenceState
] = useState(null);

If you don't want to have documents for ReferenceState , you can get rid of its @typedef and replace references to it with Object , but I think it is clearer to have docs.如果你不想有ReferenceState文档,你可以去掉它的@typedef并用Object替换对它的引用,但我认为有文档更清楚。

void above is a simpler way to say nothing special (ie, undefined ) is returned--if that's what the setter returns.上面的void是一种更简单的方式,表示不返回任何特殊内容(即undefined )——如果这就是 setter 返回的内容。 Some projects would just drop the @returns if it only returns undefined , but I like to add it to show the return value is known to be undefined and not merely undocumented.如果@returns仅返回undefined ,则某些项目只会删除它,但我喜欢添加它以显示已知返回值是undefined ,而不仅仅是未记录的。

I would create a generic type that equals the same return type as React's useState function.我会创建一个通用类型,它等于与 React 的 useState function 相同的返回类型。

/**
 * Add this type in top of your file, or if commonly used in some types file.
 * @template T
 * @typedef {[T, import('react').Dispatch<import('react').SetStateAction<T>>]} useState
 */

Then you could use it in you React component like this:然后你可以像这样在你的 React 组件中使用它:

/** @type {useState<string>} */
const [someString, setSomeString] = useState('');

/** @type {useState<number>} */
const [someNumber, setSomeNumber] = useState(2);

Or if you want to use a custom object:或者,如果您想使用自定义 object:

/**
 * @typedef SomeOtherType
 * @property {string} property1
 * @property {number} property2
 */

/** @type {useState<SomeOtherType>} */
const [someOtherValue, setSomeOtherValue] = useState(null);

In webstorm, You can write like this (I haven't tested it in other editors):在webstorm中,你可以这样写(我没有在其他编辑器中测试过):

const [state, setState] = useState(/** @type {{name: string, age: number?}} */null)

or或者

/**
 * @typedef People
 * @property {string} name
 * @property {number} [age]
 */

//........

const [state, setState] = useState(/** @type {People} */null)

As an alternative, the variables can be declared prior to destructuring and annotated with JSDoc as normal.作为替代方案,可以在解构之前声明变量,并照常使用 JSDoc 进行注释。

/**
* Current state.
* @type {Object}
*/
let referenceState;
/**
* Current state setter.
* @type {Function}
* @param {any} state updated state value.
* @returns void
*/
let setReferenceState;
[referenceState, setReferenceState] = useState(null);

The shortest way is this: but adding a template最短的方法是这样的:但是添加一个模板

/**
 * @type {[MyType, React.Dispatch<MyType>]} state
 */
const [value, setValue] = useState(null);

I also use *.d.ts files in the js projects.我还在 js 项目中使用 *.d.ts 文件。 VS code sees them and uses them so you can describe types and interfaces in a modern way. VS 代码可以看到并使用它们,因此您可以以现代方式描述类型和接口。

Just put a line放一行就行

type useState<T> = [T, React.Dispatch<T>];

to a useState.d.ts file and then in a JS file use到 useState.d.ts 文件,然后在 JS 文件中使用

/** @type {useState<MyType>} */
const [value, setValue] = useState(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM