简体   繁体   English

React Hooks TypeScript 继承父 state 和 function 类型在子

[英]React Hooks TypeScript inheriting parent state & function types in child

I'm looking for ways to inherit parent state & function types in child component so that I do not have to redefine them in child component.我正在寻找在子组件中继承父 state 和 function 类型的方法,这样我就不必在子组件中重新定义它们。

I have parent component as follows:我的父组件如下:

const TagPopupModal: React.FC = () => {
// state
  const [addTagPressed, setAddTagPressed] = useState<boolean>(false);
  const [tagList, setTagList] = useState<userTagType['tags'][]>(
    [],
  );

// function
  function addToTagList(tag: tagListType) {
   ...
  }

return (
  ...
  <TagListView 
    addTagPressed={addTagPressed}
    tagList={tagList}
    addToTagList={addToTagList}
  />
)
}

In parent component, I have defined the types for the state and parameter type for the function.在父组件中,我定义了 state 的类型和 function 的参数类型。 Those are passed to child component.这些被传递给子组件。

Now in child component I have following:现在在子组件中,我有以下内容:

interface PropTypes {
  addTagPressed:boolean;
  tagList: Array<userTagType['tags']>;
  addToTagList: (value: taglistType) => void;
}

const TagListView: React.FC<PropTypes> = ({
  addTagPressed, 
  tagList,
  addToTagList,
}) =>{
   ...
}

As you can see I had to define the same types for the props again, but using interface, in the child component.如您所见,我必须在子组件中再次为道具定义相同的类型,但使用接口。 This is time-consuming, is there ways to directly inherit the types of the state and functions from the parent?这很耗时,有没有办法直接从父级继承 state 的类型和函数?

What would be the most efficient way?什么是最有效的方法?

Thanks,谢谢,

You could export your interface for PropTypes and consume that elsewhere, eg as const [tagList, setTagList] = useState<PropTypes['tagList']>([]);您可以导出 PropTypes 的接口并在其他地方使用,例如const [tagList, setTagList] = useState<PropTypes['tagList']>([]);

You won't necessarily save much raw keyboard mashing, but it will help keep your types consistent and reusable.您不一定会节省很多原始键盘混搭,但它有助于保持您的类型一致和可重用。

import { TagListView, PropTypes } from '../TagListView';

const TagPopupModal: React.FC = () => {
// state
  const [addTagPressed, setAddTagPressed] = useState(false);
  const [tagList, setTagList] = useState<PropTypes['tagList']>(
    [],
  );

// function
  const addToTagList: PropTypes['addToTagList'] = (tag) => {
   ...
  }

return (
  ...
  <TagListView 
    addTagPressed={addTagPressed}
    tagList={tagList}
    addToTagList={addToTagList}
  />
)
}

Note that you don't need to be super vigilant about declaring explicit types in general.请注意,一般来说,您不需要对声明显式类型非常警惕。 useState(false) for example will automatically infer the type of that state as boolean .例如useState(false)将自动推断 state 的类型为boolean And if you declare the function addToTagList as a const and set its type to be what is defined in PropTypes , TS will automatically know that the parameter tag is supposed to have a given type.如果您将 function addToTagList声明为 const 并将其类型设置为PropTypes中定义的类型,TS 将自动知道参数tag应该具有给定的类型。

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

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