Extending React components in TypeScript

后端 未结 2 1533
离开以前
离开以前 2020-12-28 15:54

I\'m using React.js with TypeScript. Is there any way to create React components that inherit from other components but have some additional props/states?

What I\'m

2条回答
  •  误落风尘
    2020-12-28 16:23

    import { Component } from 'react'
    
    
    abstract class TestComponent

    extends Component { abstract test(): string } type Props = { first: string, last: string, } type State = { fullName: string, } class MyTest extends TestComponent { constructor(props: Props) { super(props) this.state = { fullName: `${props.first} ${props.last}` } } test() { const { fullName } = this.state return fullName } }

提交回复
热议问题