简体   繁体   中英

Use typescript generic to connect props to state in React class component

Is there a way for me to make the state type depend on the prop type passed in?

type FooProps = {
  allItems: any[]
}
type FooState = {
  items: any[]
}
export default class Foo extends React.Component<FooProps, FooState> {

Have you tried it?

interface FooProps<T> {
    allItems: T[]
}

interface FooState<T> {
    items: T[]
}

export default class Foo<T> extends React.Component<FooProps<T>, FooState<T>> {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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