Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Props with Generics with React.memo

I am trying to convert the following to use React.memo:

interface Props<TRowData> {
  // props...
}

export function Table<TRowData>({
  propA,
  propB
}: Props<TRowData>) {

}

Like so (incorrect):

interface Props<TRowData> {
  // props...
}



export const Table = memo<Props<TRowData>>(
({
  propA,
  propB
}) => {

})

How can I correct this syntax? Currently it has this error:

// Cannot find name 'TRowData'.
export const Table = memo<Props<TRowData>>(
                                ~~~~~~~~
like image 527
Dominic Avatar asked Feb 01 '26 02:02

Dominic


2 Answers

I solved it by keeping the memo and non-memo separate and doing some type casting:

const TableComponent = <T,>(props: TableComponentProps<T>) => {
 // ...
}

// or
function TableComponent<T>(props: TableComponentProps<T>) {
  // ...
}

Then:

export const Table = memo(TableComponent) as typeof TableComponent

Or:

const typedMemo: <T>(c: T) => T = React.memo
export const Table = typedMemo(TableComponent)
like image 61
Dominic Avatar answered Feb 03 '26 16:02

Dominic


With current React type declarations, it is not possible to create a generic component out of React.memo. A solution without type assertions is to add an additional memo function overload to leverage TS 3.4 higher order function type inference:

import React, { memo } from "react"

declare module "react" { // augment React types
  function memo<A, B>(Component: (props: A) => B): (props: A) => B
  // return type is same as ReturnType<ExoticComponent<any>>
}

You then will be able to make Table component generic. Just make sure to pass a generic function to memo:

interface Props<T> {
  a: T
}

const TableWrapped = <T extends {}>(props: Props<T>) => <div>{props.a}</div>

const Table = memo(TableWrapped)

const App = () => (
  <>
    <Table a="foo" /> {/* (props: Props<string>) => ... */}
    <Table a={3} /> {/* (props: Props<number>) => ... */}
  </>
)

Playground

like image 30
ford04 Avatar answered Feb 03 '26 16:02

ford04



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!