如何声明子类的不同状态和道具与typescript作出反应

时间:2018-06-15 04:48:48

标签: reactjs typescript

通常使用React对打字稿和静态类型进行超级新手,所以

鉴于我有这个父组件:

interface ChildProps {
 something: string
}

interface ChildState {
 somethingElse: string
}

export default class ChildComponent extends ParentComponent<ChildProps, ChildState> {
  // Child component logic.
}

如何在子组件上声明不同的道具和状态,例如:

[ts] Type 'ParentComponent' is not generic.

当我尝试上述内容时,我得到了:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

TalentSearch = TalentSearchInput.objects.all()
paginator = Paginator(TalentSearch,items_per_page)
page_num = 1

try:
    TalentSearch = paginator.page(page_num)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    TalentSearch = paginator.page(1)
except EmptyPage:
    # If page is out of range, deliver last page of results.
    TalentSearch = paginator.page(paginator.num_pages)

serializer = TalentSearchSerializer(TalentSearch, many=True)
d = Response(serializer.data)
return d

那么,实现这种分离的最佳方式是什么?,谢谢。

1 个答案:

答案 0 :(得分:1)

您也可以ParentComponent通用,以便ChildComponent可以指定不同的道具和状态:

interface ParentProps {
    something: string
}

interface ParentState {
    somethingElse: string
}

export class ParentComponent<TProps extends ParentProps = ParentProps, TState extends ParentState = ParentState>
extends React.Component<TProps, TState> {

    // Component Logic.

    sharedFunction() {
        // Some shared function.
    }
}

interface ChildProps {
    something: string
}

interface ChildState {
    somethingElse: string
}

export class ChildComponent extends ParentComponent<ChildProps, ChildState> {
    // Child component logic.
}