简体   繁体   English

如何将组件的类型断言为 prop?

[英]How do you assert the type of a component as prop?

I am trying to pass a component as prop.我正在尝试将组件作为道具传递。

The component:组件:

export type ItemProps= {
  //...
};

export const Item = ({ /* ... */ }: ItemProps) => {
  return (
    //...
  );
};

Where I'm attempting to pass it:我试图通过它的地方:

type ParentProps= {
  //...
  item: ReactElement<ItemProps>;
};

export const Parent = ({ /* ... */, item }: ParentProps) => {
  return (
    <>
      {item}
    </>
  );
};

What I'm trying to achieve:我想要达到的目标:

export const Main = () => {
  return (
    <Parent item={ <Item /> } />
  );
};

As you can see, I am trying to pass the Item component as a prop in the Parent component.如您所见,我试图将Item组件作为 prop 传递给Parent组件。 As of now, this works , but it seems that using ReactElement loosens the type to ReactElement<ItemProps, string | JSXElementConstructor<any>>截至目前,这是可行的,但似乎使用 ReactElement 将类型放松为ReactElement<ItemProps, string | JSXElementConstructor<any>> ReactElement<ItemProps, string | JSXElementConstructor<any>> , or more generally: ReactElement<ItemProps, string | JSXElementConstructor<any>> ,或更一般地说:

ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> . ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>>

The issue: if I were to replace with any other component, there wouldn't be any explicit warning or error about the component being different.问题:如果我要替换任何其他组件,则不会有任何关于组件不同的明确警告或错误。 I am trying to instead narrow it such that only the <Item /> component can be passed for the item={} prop of <Parent /> .我试图缩小它的范围,以便只有<Item />组件可以传递给<Parent />item={}道具。

In essence, what are some ways that can explicitly define a specific React component as a prop for a parent React component?本质上,有哪些方法可以显式地将特定的 React 组件定义为父 React 组件的 prop?

Because the Component returns JSX.Element , And the JSX.Element was extended from ReactElement .因为 Component 返回JSX.Element ,而JSX.Element是从ReactElement扩展而来的。

interface Element extends React.ReactElement<any, any> { }

And I think if you want to assert the component that receives from the parent Component, you can receive the complete component like this而且我认为如果你想断言从父组件接收的组件,你可以像这样接收完整的组件

暂无
暂无

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

相关问题 你如何从 React 组件中提取特定的 TypeScript prop 类型? - How do you extract a specific TypeScript prop type from a React component? 你如何键入一个作为道具传递给组件的 useState 钩子,typescript + react - How do you type a useState hook that is being passed as a prop to a component, typescript+react 如何使用defaultProps从React组件中提取道具类型? - How do you extract prop types from a React Component with defaultProps? 你如何导出道具名称和类型而不是到处定义它? - How do you export a prop name and the type instead of defining it everywhere? 当您无权访问 HOC 时,如何将组件属性输入到高阶组件? - How to type a component prop to a higher order component when you don't have access to the HOC? 如何为React props定义TypeScript类型,只有在将prop A传递给组件时,才会接受prop B? - How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component? 当使用Route render prop而不是Route component prop时,如何访问react-router-dom 4中的url args? - How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop? 如何对子组件执行道具类型验证? - How to perform prop type validation for children component? 如何在组件(React + TypeScript)中输入链接属性? - How to type link prop in component (React + TypeScript)? 在 React/Jest 单元测试中,你如何模拟被模拟组件调用的事件道具? - In a React/Jest unit test, how do you simulate an event prop being called by a mocked component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM