【发布时间】:2020-07-02 08:02:51
【问题描述】:
这是 react-native 问题,但类似的概念也可以应用于 react。
我想在 react-native 中创建一个 CustomView。我正在使用打字稿。
到目前为止,我有:
const styles = StyleSheet.create({
container: {
backgroundColor: '#ffffff',
borderRadius: 10,
}
});
type CustomViewProps= {
width: number,
height: number,
marginTop?: string | number,
}
const CustomView = ({ width, height, marginTop }: CustomViewProps) => (
<View style={[styles.container, { height, width, marginTop }]} />
);
到目前为止还可以,因为只使用了 3 个道具:width、height 和 marginTop。
但是,这是不可重复使用的,如果我需要添加更多道具,它可能会变得冗长。
所以,问题是:如何让 CustomView 接收任何 props 作为原生组件 View 可以接收的?
我的猜测是我应该删除 CustomViewProps。然后,我应该让 props 继承自原生组件 View 的相同类型。但是,我正在为此苦苦挣扎。
【问题讨论】:
标签: javascript reactjs typescript react-native types