繁体   English   中英

在 React class 组件中使用 typescript generic 将 props 连接到 state

[英]Use typescript generic to connect props to state in React class component

我有没有办法让 state 类型取决于传入的道具类型?

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

你试过了吗?

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

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

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

暂无
暂无

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

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