简体   繁体   English

类型“未定义”不可分配给类型“RefObject”<htmldivelement | undefined> '</htmldivelement>

[英]Type 'undefined' is not assignable to type 'RefObject<HTMLDivElement | undefined>'

I have a tsx file where the code is like below:我有一个 tsx 文件,其中代码如下:

interface CommandProps {
    otherStuff: string;
    commandContainerRef: React.RefObject<HTMLDivElement | undefined>;
}

const getComponentProps = (): CommandProps => {
    return {
        otherStuff: 'stuff',
        commandContainerRef: undefined,
    };
};

.....

It gave me the error:它给了我错误:

Type 'undefined' is not assignable to type 'RefObject<HTMLDivElement |类型“未定义”不可分配给类型“RefObject<HTMLDivElement | undefined>'.未定义>'。

Then, I tired commandContainerRef: React.useRef(null), the above error is gone, but it gave me the eslint error below:然后,我累了 commandContainerRef: React.useRef(null),上面的错误消失了,但是它给了我下面的 eslint 错误:

Error - React Hook "React.useRef" is called in function "getComponentProps" that is neither a React function component nor a custom React Hook function.错误 - 在 function “getComponentProps” 中调用了 React Hook “React.useRef”,它既不是 React function 组件也不是自定义 React Hook ZC1C425268E68385D1AB50741C React component names must start with an uppercase letter. React 组件名称必须以大写字母开头。 React Hook names must start with the word "use". React Hook 名称必须以“use”开头。 (react-hooks/rules-of-hooks) (反应钩子/钩子规则)

I am wondering how to address this issue.我想知道如何解决这个问题。 Thanks in advance!提前致谢!

React.RefObject<T> is an interface with a read-only property named current of the given type T (or null ), in this case CommandProps or undefined (or null ). React.RefObject<T>是一个具有只读属性的接口,名为current的给定类型T (或null ),在本例中为CommandPropsundefined (或null )。

So, if you want commandContainerRef returned from getComponentProps to be a React.RefObject to undefined this should work:因此,如果您希望从getComponentProps返回的commandContainerRefundefinedReact.RefObject ,则应该可以:

const getComponentProps = (): CommandProps => {
    return {
        otherStuff: 'stuff',
        commandContainerRef: { current: undefined },
    };
};

Or, depending on what your precise need is, you could alternatively change commandContainerRef to:或者,根据您的确切需求,您也可以将commandContainerRef更改为:

commandContainerRef: React.RefObject<HTMLDivElement> | undefined;

Then the getComponentProps implementation in the question should work.那么问题中的getComponentProps实现应该可以工作。

暂无
暂无

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

相关问题 类型“字符串”不可分配给类型“未定义” - Type 'string' is not assignable to type ' undefined' 类型 GQLSchema 不可分配给类型 undefined - Type GQLSchema is not assignable to type undefined 类型“未定义”不可分配给类型“日期” - Type 'undefined' is not assignable to type 'Date' TypeScript:未定义不可分配给类型“布尔|” 连接选项 | 不明确的 - TypeScript: undefined is not assignable to type 'boolean | ConnectionOptions | undefined 输入'{class:字符串; }' 不可分配给类型 'DetailedHTMLProps <htmlattributes<htmldivelement> , HTMLDivElement>' </htmlattributes<htmldivelement> - Type '{ class: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>' 类型 '((instance: HTMLDivElement | null) =&gt; void) | 上不存在属性 'current' 参考对象<htmldivelement> '</htmldivelement> - Property 'current' does not exist on type '((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement>' 键入'字符串 | undefined' 不可分配给类型 'string' - Type 'string | undefined' is not assignable to type 'string' TypeScript - 类型“未定义”不可分配给类型“ReactElement” - TypeScript - Type 'undefined' is not assignable to type 'ReactElement' 打字稿错误:类型&#39;undefined []&#39;不能分配给&#39;void&#39;类型 - Typescript error: Type 'undefined[]' is not assignable to type 'void' 类型 &#39;undefined&#39; 不能分配给类型 &#39;number&#39; .ts(2322) - Type 'undefined' is not assignable to type 'number' .ts(2322)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM