如何通过父对象字段中的子子组件扩展父级?

问题描述 投票:0回答:1
// Component.js interface Props { children: React.ReactNode } const Parent: React.FC<Props> & { Item?: React.FC } = ({ children }) => { return <div>{children}</div>; } const Child: React.FC<Props> = () => { return <span>Child</span>; } Parent.Item = Child; export default Parent; // App.js import { default as Parent } from 'component.js'; const { Item } = Parent; const App = () => { return ( <Parent> <Item/> {/* Typescript error here */} <Item/> {/* Typescript error here */} <Item/> {/* Typescript error here */} </Parent> ); }

发生这种误差是因为打字稿将其视为可选的,导致其不确定。修复程序是键入为

Item

,确保始终存在。这消除了类型误差,并允许
Parent
用作有效的反应组件。
reactjs typescript
1个回答
0
投票


最新问题
© www.soinside.com 2019 - 2024. All rights reserved.