简体   繁体   English

在这个例子中如何使用 useMemo?

[英]How to use useMemo in this example?

I'm struggling with problem chaning const variable to const with useMemo.我正在努力解决使用 useMemo 将const variable更改为 const 的问题。 I tried some example from docs and some tutorials and nothing worked for me.我尝试了一些文档和一些教程中的示例,但对我没有任何帮助。 This have to be easy...这必须很容易...

const bottomSheetOptions: BottomSheetAction[] = [
    {
      label:
        t('kebabMenu.send') +
        (count > 1 ? t('kebabMenu.documents', { count }) : ''),
      icon: <SendIcon />,
      state: sendVisible,
      dismiss: () => showSendAlert(),
      onClick: () => showSendAlert(),
      alertType: 'sendInvoice',
      disabled: count > 0,
      visible: documentType === 'sales'
    },
    {
      label: t('kebabMenu.download'),
      icon: <DownloadIcon />,
      state: downloadVisible,
      dismiss: () => showDownloadDocumentsModal(),
      onClick: () => showDownloadDocumentsModal(),
      alertType: 'download',
      disabled: true,
      visible: true
    }
  ];

So i'm tried this but it gives me error所以我试过了,但它给了我错误

const bottomSheetOptions: BottomSheetAction[] = useMemo(() => [[
    {
      label:
        t('kebabMenu.send') +
        (count > 1 ? t('kebabMenu.documents', { count }) : ''),
      icon: <SendIcon />,
      state: sendVisible,
      dismiss: () => showSendAlert(),
      onClick: () => showSendAlert(),
      alertType: 'sendInvoice',
      disabled: count > 0,
      visible: documentType === 'sales'
    },
    {
      label: t('kebabMenu.download'),
      icon: <DownloadIcon />,
      state: downloadVisible,
      dismiss: () => showDownloadDocumentsModal(),
      onClick: () => showDownloadDocumentsModal(),
      alertType: 'download',
      disabled: true,
      visible: true
    }
  ]], [documentType, downloadVisible, sendVisible, showDownloadDocumentsModal, showSendAlert]) 

Error:错误:

Type '{ label: string; icon: Element; state: boolean; dismiss: () => void; onClick: () => void; alertType: string; disabled: boolean; visible: boolean; }[][]' is not assignable to type 'BottomSheetAction[]'.
  Type '{ label: string; icon: Element; state: boolean; dismiss: () => void; onClick: () => void; alertType: string; disabled: boolean; visible: boolean; }[]' is missing the following properties from type 'BottomSheetAction': label, icon, state, dismiss, and 4 more.

---EDIT--- Adding type of array BottomSheetAction ---EDIT--- 添加数组BottomSheetAction的类型

export type BottomSheetAction = {
  label: string;
  icon: React.ReactNode;
  state: boolean;
  dismiss: () => void;
  onClick: () => void;
  alertType: 'download' | 'sendInvoice';
  disabled: boolean;
  visible: boolean;
};

I think that the type of bottomSheetOptions which is BottomSheetAction[] doesn't match what you return inside the useMemo callback.我认为bottomSheetOptions的类型BottomSheetAction[]与您在useMemo回调中返回的内容不匹配。

I think that you are returning an array which contains a single array which contains items of type BottomSheetAction .我认为您正在返回一个数组,其中包含一个包含BottomSheetAction类型的项目的数组。

Could you try changing [[ to [ and ]] to ] ?您可以尝试将[[更改为[]]更改为]吗?

In your "useMemo" attempt you are returning an array in an array.在您的“useMemo”尝试中,您将返回一个数组中的数组。 I don't have the BottomSheetAction type, but try replacing the outer square bracket with round ones, ie:我没有BottomSheetAction类型,但尝试用圆括号替换外部方括号,即:

const bottomSheetOptions: BottomSheetAction[] = useMemo(() => [
    {
      label:
        t('kebabMenu.send') +
        (count > 1 ? t('kebabMenu.documents', { count }) : ''),
      icon: <SendIcon />,
      state: sendVisible,
      dismiss: () => showSendAlert(),
      onClick: () => showSendAlert(),
      alertType: 'sendInvoice',
      disabled: count > 0,
      visible: documentType === 'sales'
    },
    {
      label: t('kebabMenu.download'),
      icon: <DownloadIcon />,
      state: downloadVisible,
      dismiss: () => showDownloadDocumentsModal(),
      onClick: () => showDownloadDocumentsModal(),
      alertType: 'download',
      disabled: true,
      visible: true
    }
  ], [documentType, downloadVisible, sendVisible, showDownloadDocumentsModal, showSendAlert])

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

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