gpt4 book ai didi

reactjs - 基于多个 Typescript 接口(interface)的 React prop 分离

转载 作者:行者123 更新时间:2023-12-05 08:26:14 25 4
gpt4 key购买 nike

在 React 中有没有一种方法可以根据扩展多个其他接口(interface)的 Typescript 接口(interface)来分离 props 对象?我看到的另一种方法是将重复的 Prop 传递给不会使用它们的组件,这不是最佳解决方案。

interface IProps extends IAProps, IBProps {}

const Component1: SFC<IProps> = (props) =>
return (
<Component2
{...props} <--- only IAProps
/>
<Component3
{...props} <--- only IBProps
/>
);
}

最佳答案

您可以使用 &合并接口(interface)。如<ScreenProps extends {} & SliderProps & ReactNavigationProps>

例子:


interface AProps {
testa: any
}

interface BProps {
testb: any
}


class Test extends Component<AProps & BProps> {
// ...
}


// or


<IProps extends AProps & BProps>

class Test extends Component<IProps> {
// ...
}


关于reactjs - 基于多个 Typescript 接口(interface)的 React prop 分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55969554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com