繁体   English   中英

使用Typescript时React.memo不可用吗?

[英]React.memo is not available when working with Typescript?

我试图像这样使用React.memo:

const Node: React.memo<Props> = props => {

但我收到一个错误消息:

Namespace '../ui/node_modules/@types/react/index.d.ts"' has no exported member 'memo'.

但是我可以在index.d.ts中看到Memo函数:

function memo<P extends object>(
    Component: SFC<P>,
    propsAreEqual?: (prevProps: Readonly<PropsWithChildren<P>>, nextProps: Readonly<PropsWithChildren<P>>) => boolean
): NamedExoticComponent<P>;

function memo<T extends ComponentType<any>>(
    Component: T,
    propsAreEqual?: (prevProps: Readonly<ComponentProps<T>>, nextProps: Readonly<ComponentProps<T>>) => boolean
): MemoExoticComponent<T>;

我在这里想念什么?

如评论中所述, memo不是类型声明。 您的语法有点混乱

const Node: React.FC<Props> = React.memo(props => {
  return(
      <div>
        memoized
      </div>
  )
})

请注意, React.FC是功能组件的声明。 由于您将其包装在memo ,因此可能需要更改类型。 但就我个人而言,我并不需要。

暂无
暂无

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

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