IT이야기

React/TypeScript: 추가 속성으로 구성 요소 확장

cyworld 2022. 3. 18. 21:48
반응형

React/TypeScript: 추가 속성으로 구성 요소 확장

내 전류 구성 요소(순수 형식)를 재생성하기 위해 리액션을 사용하려고 하지만 다른 구성 요소를 확장하는 구성 요소에 추가 소품을 제공할 방법을 찾을 수 없다.

export interface DataTableProps {
    columns: any[];
    data: any[];
}

export class DataTable extends React.Component<DataTableProps, {}> {
   render() {
       // -- I can use this.props.columns and this.props.data --
   }
}

export class AnimalTable extends DataTable {
    render() {
       // -- I would need to use a this.props.onClickFunction -- 
    }
}

내 문제는 AnimalTable에 DataTable과 무관한 소품을 줘야 한다는 거야.내가 어떻게 그럴 수 있을까?

네가 만들어야 할 것이다.DataTable확장 가능한 인터페이스를 사용할 수 있도록 일반적DataTableProps:

export interface AnimalTableProps extends DataTableProps {
    onClickFunction: Function;
}

export class DataTable<T extends DataTableProps> extends React.Component<T, {}> { }

export class AnimalTable extends DataTable<AnimalTableProps> {
    render() {
        // this.props.onClickFunction should be available
    }
}

필요한 경우 기본 클래스는 모든 인스턴스가 구현해야 하는 필수/추상 방법을 선언할 수 있다.

import { Component } from 'react'


abstract class TestComponent<P = {}, S = {}, SS = any> extends Component<P, S, SS> {
  abstract test(): string
}


type Props = {
  first: string,
  last: string,
}

type State = {
  fullName: string,
}

class MyTest extends TestComponent<Props, State> {
  constructor(props: Props) {
    super(props)
    this.state = {
      fullName: `${props.first} ${props.last}`
    }
  }

  test() {
    const { fullName } = this.state
    return fullName
  }
}

(별도의 일반 클래스 없이) 내가 찾은 가장 우아한 해결책은

interface IBaseProps {
    name: string;
}

class Base<P> extends React.Component<P & IBaseProps, {}>{

}

interface IChildProps extends IBaseProps {
    id: number;
}

class Child extends Base<IChildProps> {
    render(): JSX.Element {
        return (
            <div>
                {this.props.id}
                {this.props.name} 
            </div>
        );
    }
}

경험상 유산을 피하는 것이 좋을 것이다.운 좋게도 TS와 리액션은 그것을 가능하게 하는 훌륭한 도구다. (예를 들어, 유산이 종종 당신에게 보일러 한 다발을 절약하는 c#와는 달리)

export interface DataTableProps {
    columns: any[];
    data: any[];
}

export class DataTable extends React.Component<DataTableProps, {}> {
   render() {
       // -- I can use this.props.columns and this.props.data --
   }
}

export type AnimalTableProps = DataTableProps & {
    onClickFunction: () => void;
};

export class AnimalTable extends React.Component<AnimalTableProps, {}> {
    render() {
        const {onClickFunction, ...tableProps} = this.props;
        // use onClickFunction however you need it
        return <DataTable {...tableProps}></DataTable>
    }
}

상태 및 소품에서 확장 및 유지 관리할 수 있는 구성 요소 생성의 전체 예

import { Component } from "react";

// Props for the Base Component
export interface BaseComponentProps { }

// State for the Base Component
export interface BaseComponentState {
    isLoaded?: boolean
}

// The Base Component that your components can extend
export class BaseComponent<Props extends BaseComponentProps, State extends BaseComponentState, SS = any> extends Component<Props, State, SS> {

    State: BaseComponentState = {
        isLoaded: false
    }

    constructor(props: Props) {
        super(props);
    }

    componentDidMount() {
        this.setState({ isLoaded: true })
    }
}

// Props for your specialized component
export interface MainComponentProps extends BaseComponentProps {

}

// State for your specialized component
export interface MainComponentState extends BaseComponentState {
    CanRead: boolean
}

// Your component which now extends the BaseComponent
export class MainComponent extends BaseComponent<MainComponentProps, MainComponentState> {
    state: MainComponentState = {
        CanRead: false
    }

    componentDidMount() {
        super.componentDidMount();

        if (this.state.isLoaded) {
            this.setState({ CanRead: true })
        }
    }
}

참조URL: https://stackoverflow.com/questions/39123667/react-typescript-extending-a-component-with-additional-properties

반응형