传播类型只能从对象类型创建

问题描述 投票:0回答:1

我有一个使用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
reactjs typescript react-native ecmascript-6 spread-syntax
1个回答
1
投票

您需要使用数组语法在react native中提供多个样式

<Image style={[{ width: x, height; y}, style]} />

the docs for more info

© www.soinside.com 2019 - 2024. All rights reserved.