简体   繁体   English

如何修复错误类型 void is not assignable to type using react and typescript?

[英]How to fix error type void is not assignable to type using react and typescript?

i have the code like below which works.我有如下有效的代码。

const { loading, data, error, refetch } = useItemDetailsQuery({
    variables,
    notifyOnNetworkStatusChange: true,
    fetchPolicy: 'network-only',
});

const details: Partial<Item> = data?.itemDetails
    ? data.itemDetails
    : {};

type Item = ItemDetailsQuery['itemDetails'];

type itemDetailsQuery = {
    itemDetails: Pick<
        Types.ItemDetails,
        | 'created'
        | 'description'
        | 'name'
    >
 };

Now with above code, the data in details doesnt get updated page refresh.现在使用上面的代码,详细数据不会更新页面刷新。 so i am thinking to put the calculation logic of details in useMemo such that it calculates this logic when data changes and hence there would be no need to reload the page to view new data.所以我想把细节的计算逻辑放在 useMemo 中,这样它就可以在数据发生变化时计算这个逻辑,因此不需要重新加载页面来查看新数据。

so i tried putting the logic in useMemo like so所以我试着像这样把逻辑放在 useMemo 中

const details: Partial<Item> = React.useMemo(
    () => { data?.itemDetails
        ? data.itemDetails
        : {}
    }, [data]
);

but this gives me error "Type 'void' is not assignable to type 'Partial<Pick<ItemDetails, "created", "description", "name"}>"但这给了我错误“类型'void'不可分配给类型'Partial <Pick <ItemDetails,“created”,“description”,“name”}>”

I am not sure how to fix this problem.我不确定如何解决这个问题。 could someone help me with this.有人可以帮我解决这个问题吗? thanks.谢谢。

I am new to using react and typescript. In the process of learning.刚接触react和typescript,正在学习中。

you forgot the return in useMemo.你忘记了 useMemo 中的回报。 Without return in the curry function, the value of details will be undefined type as void.如果没有在 curry function 中返回,details 的值将是未定义类型的 void。

const details: Partial<Item> = React.useMemo(
    () => { 
        return data?.itemDetails
        ? data.itemDetails
        : {}
    }, [data]
);

暂无
暂无

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

相关问题 使用反应成帧器的 typescript 中的错误类型“()=&gt; void”不可分配给类型“未定义” - Error in typescript using react framer Type '() => void' is not assignable to type 'undefined' 如何修复错误类型 void is not assignable to type ((event: MouseEvent<htmldivelement, mouseevent> ) =&gt; void) |未定义使用反应?</htmldivelement,> - How to fix the error type void is not assignable to type ((event: MouseEvent<HTMLDivElement, MouseEvent>) => void) |undefined using react? 将 React useMemo() 与 TypeScript 一起使用时,无法分配给类型 '() => void' 错误? - not assignable to type '() => void' error when using React useMemo() with TypeScript? 如何使用react和typescript修复错误“类型参数(打开:任何)=&gt;布尔值不能分配给布尔类型的参数”? - How to fix error "argument of type (open: any) => boolean is not assignable to parameter of type boolean" using react and typescript? 如何修复错误类型 Item[] | undefined 不能使用 react 和 typescript 分配给 Item[] 类型? - How to fix the error type Item[] | undefined is not assignable to Item[] type using react and typescript? 如何使用 typescript 修复字符串类型的错误参数或未定义的参数不可分配给字符串类型的参数并做出反应? - How to fix the error argument of type string or undefined is not assignable to parameter of type string using typescript and react? 无效类型不能分配给布尔类型(反应/打字稿) - Type Void is Not Assignable to Type Boolean (React/Typescript) 如何使用 react 和 typescript 修复错误“false 类型的参数不可分配给 (currentValue: boolean) =&gt; boolean”? - How to fix error “argument of type false is not assignable to (currentValue: boolean) => boolean” using react and typescript? 反应 Redux 连接不可分配给类型 '() =&gt; void' 与 typescript - React Redux connect is not assignable to type '() => void' with typescript 类型 'void' 不可分配给类型 '((event: ChangeEvent<htmlinputelement> ) =&gt; void) 反应 TypeScript</htmlinputelement> - Type 'void' is not assignable to type '((event: ChangeEvent<HTMLInputElement>) => void) React TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM