有没有办法检查或验证直接来自相关类型或接口的 props。

huangapple go评论135阅读模式
英文:

Is there a way to check or validate the props coming through directly from the related type or Interface

问题

我有这个简单的组件,它的props包括一个字符串数组(标签)和子组件(要渲染的组件)。我想直接从类型声明中检查提供的子组件是否与标签数组的长度相同,或者有任何更干净的方法来处理这个检查。

type TabsProps = {
tabs: string[];
children: ReactNode | ReactNode[];
};

const Tabs = ({ tabs, children }: TabsProps) => {
const [activeTab, setActiveTab] = useState(0);

const componentsToRender = Children.toArray(children);

//TODO 以更干净的方式处理这个检查
if (componentsToRender.length !== tabs.length) {
throw new Error('Your components or tabs are not of equal length');
}

const handleTabSwitch = (index: number) => {
setActiveTab(index);
};

return (




{componentsToRender.map((component, i) => {
return activeTab === i ? component : null;
})}


);
};

export default Tabs;



<details>
<summary>英文:</summary>

I have this simple component which takes as props an array of strings (tabs) and children (component to be rendered). I want to check if the provided children are of the same length as the tabs array directly from the type declaration or any cleaner way to handle this check. 

type TabsProps = {
tabs: string[];
children: ReactNode | ReactNode[];
};

const Tabs = ({ tabs, children }: TabsProps) => {
const [activeTab, setActiveTab] = useState(0);

const componentsToRender = Children.toArray(children);

//TODO Handle this check in a cleaner way
if (componentsToRender.length !== tabs.length) {
throw new Error('Your components or tabs are not of equal length');
}

const handleTabSwitch = (index: number) => {
setActiveTab(index);
};

return (
<TabWrapper>
<TabHeader tabs={tabs} activeTab={activeTab} onTabSwitch={handleTabSwitch} />
<Dividers />
<TabContent>
{componentsToRender.map((component, i) => {
return activeTab === i ? component : null;
})}
</TabContent>
</TabWrapper>
);
};

export default Tabs;



As shown in the code snippet, I handled it with a simple if condition that throws an error
But I would rather it be handled while coding instead of at runtime

</details>


# 答案1
**得分**: 1

可以定义一个类型,用于检查子数组的长度是否与标签数组的长度相匹配:

type TabsProps<T extends string[]> = {
  tabs: T;
  children: { [K in keyof T]: ReactNode };
};

const Tabs = <T extends string[]>({ tabs, children }: TabsProps<T>) => {
  const [activeTab, setActiveTab] = useState(0);

  const componentsToRender = Children.toArray(children);

  const handleTabSwitch = (index: number) => {
    setActiveTab(index);
  };

  return (
    <TabWrapper>
      <TabHeader tabs={tabs} activeTab={activeTab} onTabSwitch={handleTabSwitch} />
      <Dividers />
      <TabContent>
        {componentsToRender.map((component, i) => {
          return activeTab === i ? component : null;
        })}
      </TabContent>
    </TabWrapper>
  );
};

export default Tabs;
英文:

you can define a type that checks whether the length of the array of children matches the length of the array of tabs :

type TabsProps&lt;T extends string[]&gt; = {
  tabs: T;
  children: { [K in keyof T]: ReactNode };
};

const Tabs = &lt;T extends string[]&gt;({ tabs, children }: TabsProps&lt;T&gt;) =&gt; {
  const [activeTab, setActiveTab] = useState(0);

  const componentsToRender = Children.toArray(children);

  const handleTabSwitch = (index: number) =&gt; {
    setActiveTab(index);
  };

  return (
    &lt;TabWrapper&gt;
      &lt;TabHeader tabs={tabs} activeTab={activeTab} onTabSwitch={handleTabSwitch} /&gt;
      &lt;Dividers /&gt;
      &lt;TabContent&gt;
        {componentsToRender.map((component, i) =&gt; {
          return activeTab === i ? component : null;
        })}
      &lt;/TabContent&gt;
    &lt;/TabWrapper&gt;
  );
};

export default Tabs;

huangapple
  • 本文由 发表于 2023年3月8日 18:14:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定