我有一个使用ts的简单react-native组件,它指定了自己的样式,但是我也希望从组件树上面传递我可能使用的任何样式(即我正在使用此组件的某个地方)
class RatioImage extends React.Component<Props> {
render() {
const { width, ratio, style, ...props } = this.props;
return (
<Image
{...props}
style={{
width: deviceWidth * (width / 100),
height: deviceWidth * (width / 100) * ratio,
...style
}}
/>
);
}
}
我目前得到的错误在我的...style
不知道为什么,因为它应该是一个对象?
[ts] Spread types may only be created from object types.
const style: false | ImageStyle | (number & {
__registeredStyleBrand: ImageStyle;
}) | RecursiveArray<false | ImageStyle | (number & {
__registeredStyleBrand: ImageStyle;
}) | null | undefined> | null | undefined
您需要使用数组语法在react native中提供多个样式
<Image style={[{ width: x, height; y}, style]} />